【问题标题】:Views not responding on action when added from framework从框架添加时视图对操作没有响应
【发布时间】:2020-07-12 12:02:08
【问题描述】:

我为我的自定义视图创建了一个简单的框架。从我的角度来看,我希望可以跨多个应用程序重用。问题是,当我在项目中更改约束时,在 textField 中输入文本时,视图没有响应任何操作,例如点击按钮。我猜问题出在translatesAutoresizingMaskIntoConstraints。

假设我有一个像下面这样的框架:

public class CustomView: UIView {


public let customButton: UIButton = {
    let button = UIButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Save", for: .normal)
    button.layer.cornerRadius = 10
    button.layer.borderWidth = 1
    button.layer.borderColor = UIColor.red.cgColor
    button.setTitleColor(.red, for: .normal)
    
    return button
}()

public var buttonWidth: CGFloat = 100

public override init(frame: CGRect) {
    super.init(frame: frame)
    initConstraints()
}


func initConstraints(){
    addSubview(customButton)
    
    NSLayoutConstraint.activate([
        
        customButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 80),
        customButton.centerXAnchor.constraint(equalTo: centerXAnchor),
        customButton.widthAnchor.constraint(equalToConstant: buttonWidth)

    ])
}

public required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}}

成功将框架添加到项目后,我需要更改视图的位置。从中心到顶部。我正在创建一个类

class FrameworkView: UIView {

let customButton: CustomView = {
    let cusomView = CustomView()
    cusomView.translatesAutoresizingMaskIntoConstraints = false
    return cusomView
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    initConst()
}

func initConst()  {
    
    addSubview(customButton)
    NSLayoutConstraint.activate([
        customButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 100),
        customButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 25),
        customButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -25),
    ])
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}}

当我将视图添加到 ViewController 时,一切看起来都正常。

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let demo = FrameworkView(frame: view.frame)

    view.addSubview(demo)
    
    
    demo.customButton.customButton.addTarget(self, action: #selector(tap(_:)), for: .touchUpInside)
}


@objc func tap(_ sender: UIButton){
    print("Test")
}

很遗憾,按钮没有响应操作。与 textFields、TextViews 等其他视图相同。 我不知道如何解决这个错误。我猜想子视图和约束有问题。 感谢您提供任何帮助。 马特

【问题讨论】:

    标签: swift xcode frameworks autolayout


    【解决方案1】:

    CustomView(FrameworkView 的子视图)的高度不明确。这会导致它不显示。但是因为UIButton的框架没有歧义,所以还是显示出来了。我认为这可能是点击事件没有传递到按钮的原因 - 按钮的超级视图的框架 CustomView 是模棱两可的。

    之所以高度不明确是因为你好像忘记了一个底部约束:

    // in FrameworkView
    NSLayoutConstraint.activate([
        customButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 100),
        customButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 25),
        customButton.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -25),
        // your forgot this!
        customButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -100)
    ])
    

    如果您添加底部约束,则按钮将起作用。


    您可以考虑修改CustomView 使其暴露topConstraint 属性,而不是创建另一个包含CustomView 的视图(因为您似乎只想将按钮向下移动一点):

    public class CustomView: UIView {
    
        public var topConstraint: NSLayoutConstraint!
    
        ...
    
        private func initConstraints(){
            addSubview(customButton)
            topConstraint = customButton.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 80)
            NSLayoutConstraint.activate([
                
                topConstraint,
                customButton.centerXAnchor.constraint(equalTo: centerXAnchor),
                customButton.widthAnchor.constraint(equalToConstant: buttonWidth)
    
            ])
        }
    

    现在你不需要FrameworkView,可以设置topConstraint.constant向下移动按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多