iOS 中的自定义视图大小不是动态的

Custom View size in iOS not dynamic

一周前,我刚刚在 iOS 上启动了我的第一个应用程序。我使用 AppleDeveloperWebsite 自定义评级控件教程创建了一个自定义视图,以便在我的应用程序中使用它。

现在我在故事板中选择了 iPhone7 设备,我在 iPhone 7 模拟器上 运行 它工作完美,但是当我 运行 它在 iPhone 5 模拟器(屏幕大小变化) 我的自定义视图超出了屏幕。当我设置约束时,我所有其他控件的大小都会调整大小,但只有我的自定义视图大小会弄乱。

请帮忙

import UIKit

@IBDesignable class xRadioButtonView: UIView {
var button: UIButton!
var label: UILabel!

@IBInspectable var text: String? {
    didSet{
        label.text = text
    }
}

//Properties
var isSelected = 0

//Initialization
override init(frame: CGRect){
    super.init(frame: frame)
    addSubviews()
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
    addSubviews()
}

func addSubviews() {
    self.backgroundColor = UIColor.white

    let xWidth = bounds.size.width
    let xHeight = bounds.size.height

    let tap = UITapGestureRecognizer(target: self, action: #selector(xRadioButtonView.radioButtonTextTapped))

    button = UIButton(frame: CGRect(x: 1, y: 1, width: xWidth - 2, height: xHeight - 4))
    button.backgroundColor = UIColor.white
    button.addTarget(self, action: #selector(xRadioButtonView.radioButtonTapped(button:)), for: .touchDown)

    addSubview(button)

    label = UILabel(frame: CGRect(x: 1, y: 1, width: xWidth - 2, height: xHeight - 2))
    label.textColor = UIColor.init(hex: "#D5D5D5")
    //label.font = UIFont.init(name: label.font.fontName, size: 25)
    //label.font = label.font.withSize(25)
    label.font = UIFont.boldSystemFont(ofSize: 25)
    label.textAlignment = NSTextAlignment.center
    label.isUserInteractionEnabled = true
    label.addGestureRecognizer(tap)

    addSubview(label)
}

override func layoutSubviews() {
    // Set the button's width and height to a square the size of the frame's height.

}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    label.text = "xRBV"
}

func radioButtonTapped(button: UIButton) {
    if isSelected == 0 {
        isSelected = 1
        self.backgroundColor = UIColor.init(hex: "#00BFA5")
        label.textColor = UIColor.init(hex: "#00BFA5")
    } else {
        isSelected = 0
        self.backgroundColor = UIColor.white
        label.textColor = UIColor.init(hex: "#D5D5D5")
    }
}

func radioButtonTextTapped(sender: UITapGestureRecognizer){
    if isSelected == 0 {
        isSelected = 1
        self.backgroundColor = UIColor.init(hex: "#00BFA5")
        label.textColor = UIColor.init(hex: "#00BFA5")
    } else {
        isSelected = 0
        self.backgroundColor = UIColor.white
        label.textColor = UIColor.init(hex: "#D5D5D5")
    }
}
}

如您所见,PG 按钮应该在绿色结束但白色按钮超出屏幕的地方结束

您要么需要在layoutSubViews中设置frame,要么需要在代码中实现autolayout:

    button = UIButton(frame: CGRect(x: 1, y: 1, width: xWidth - 2, height: xHeight - 4))
    button.backgroundColor = UIColor.white
    button.addTarget(self, action: #selector(xRadioButtonView.radioButtonTapped(button:)), for: .touchDown)
    addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    let attributes: [NSLayoutAttribute] = [.top, .bottom, .leading, .trailing]
    let constants = [2, 2, 10, 10]  // 2 from top and bottom, 10 from leading and trailing
    NSLayoutConstraint.activate(attributes.enumerated().map { NSLayoutConstraint(item: button, attribute: , relatedBy: .equal, toItem: button.superview, attribute: , multiplier: 1, constant: constants[[=10=]]) })

该示例使用旧方法,因为您的约束是统一的,但如果您有更复杂的东西,从 iOS 9.

开始使用 NSLayoutAnchor 通常更简单

编辑:如果有人感兴趣,这里是元组的代码:

    button.translatesAutoresizingMaskIntoConstraints = false
    let attributes: [(NSLayoutAttribute, CGFloat)] = [(.top, 2), (.bottom, 2), (.leading, 12), (.trailing, 12)]
    NSLayoutConstraint.activate(attributes.map { NSLayoutConstraint(item: button, attribute: [=11=].0, relatedBy: .equal, toItem: button.superview, attribute: [=11=].0, multiplier: 1, constant: [=11=].1) })

谢谢,我当时还不知道这个 NSLayout。 (HEHE 7 Days) 多亏了你,我的问题有了解决方案。虽然我想要 .top .bottom .leading .trailling

的不同值

我这样使用你的代码

 NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: button.superview, attribute: .top, multiplier: 1, constant: 1).isActive = true

 NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: button.superview, attribute: .bottom, multiplier: 1, constant: 4).isActive = true 

所有 4 个边。但是有没有一种方法可以像提供多个属性值一样提供常量值?