【问题标题】:addSubview doesn't work in UIView - Swift ProgrammaticallyaddSubview 在 UIView 中不起作用 - Swift 编程
【发布时间】:2020-12-07 01:54:58
【问题描述】:

我需要在 UIView 上添加一个 UIButton 作为子视图,但它实际上并没有在运行时出现。 这是我的代码:

let moreButton : UIButton = {
    let button = UIButton()
    button.setImage(#imageLiteral(resourceName: "more"), for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    return button
}()

 override init(frame: CGRect) {
    super.init(frame: frame)
    
    addSubview(moreButton)
    moreButton.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
    moreButton.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
    moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
    
    
}

按钮最终不会添加到视图中。我确信这是一个简单的解决方法,但我无法理解它。

【问题讨论】:

    标签: ios swift uiview constraints addsubview


    【解决方案1】:

    首先,确保 viewController 显示任何内容,因为您在没有情节提要的情况下执行此操作,请查看这个简单的教程:

    https://medium.com/better-programming/creating-a-project-without-storyboard-in-2020-and-without-swifui-82080eb6d13b

    如果问题出在那个 UIButton 上,尝试在 viewDidLoad 中设置子视图:

    final class ViewController: UIViewController {
    
    let cardView = CardView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(cardView)
        
        /// Constraints
        let margins = view.layoutMarginsGuide
        cardView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        cardView.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        cardView.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        cardView.heightAnchor.constraint(equalToConstant: 30).isActive = true
        cardView.widthAnchor.constraint(equalToConstant: 20).isActive = true
    }
    
    }
    
    final class CardView: UIView {
    
    let moreButton : UIButton = {
        let button = UIButton()
        button.setTitle("Button title", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        self.translatesAutoresizingMaskIntoConstraints = false
        
        self.addSubview(moreButton)
        
        /// Constraints
        let margins = self.layoutMarginsGuide
        moreButton.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
        moreButton.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
        moreButton.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
        moreButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
        moreButton.widthAnchor.constraint(equalToConstant: 20).isActive = true
    }
    }
    

    我已经为约束设置了边距,还添加了一个leadingAnchor 约束,这也可能是问题所在。

    【讨论】:

    • 问题是 cardView 不是 viewController 而是 UIView 所以我可以初始化按钮的唯一方法是在 init 中
    • 哦,我明白了,对不起。我已经更新了 sn-p 以查看它如何与自定义 UIView 一起使用。问题也可能是 cardView 的 translatesAutoresizingMaskIntoConstraints 或为 UIView 设置额外的约束。我已经复制了约束,但它们可能可以设置得更好
    猜你喜欢
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    相关资源
    最近更新 更多