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

时间:2016-11-10 18:21:39

标签: ios iphone swift custom-controls

我刚刚在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按钮应该在绿色完成但白色按钮延伸到屏幕之外的地方完成

2 个答案:

答案 0 :(得分:1)

您需要在layoutSubViews中设置框架,或者需要在代码中实现自动布局:

    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: $1, relatedBy: .equal, toItem: button.superview, attribute: $1, multiplier: 1, constant: constants[$0]) })

该示例使用旧方法,因为您的约束是统一的,但如果您有更复杂的东西,从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: $0.0, relatedBy: .equal, toItem: button.superview, attribute: $0.0, multiplier: 1, constant: $0.1) })

答案 1 :(得分:0)

谢谢我甚至还没有意识到这个NSLayout。 (HEH​​E 7天)感谢您,我有解决方案。虽然我想要.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方都是。但有没有办法提供常量值,就像你提供了多个属性值一样?