【问题标题】:Adding constraints in awakeFromNib() method在 awakeFromNib() 方法中添加约束
【发布时间】:2019-04-01 13:02:10
【问题描述】:

我的项目中有两个自定义视图,需要以完全相同的比例放大。所以我决定为此使用 UIScrollView,它非常适合。

我决定开发一个从 UIScrollView 继承的非常简单的类,并在其中初始化所有视图结构。这样,我就避免了 NIB 文件中的任何构造步骤,并且只需添加一个视图即可使用我的类。但是我已经在添加 contentView 的阶段遇到了一个问题。

这是我的课:

final class PlayerContentView: UIScrollView {
    fileprivate var contentView: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.backgroundColor = .clear
        setupScrollProperties()

        setupContentView()
    }

    private func setupScrollProperties()
    {
        self.translatesAutoresizingMaskIntoConstraints = false
        self.minimumZoomScale = 1.0
        self.maximumZoomScale = 2.0
        self.contentSize = frame.size
        self.delegate = self
    }

    private func setupContentView()
    {
        contentView = UIView(frame: self.frame)
        contentView.backgroundColor = .red
        self.addSubview(contentView)

        CommonSwiftUtility.setSideConstraints(superview: self, view: contentView)
        CommonSwiftUtility.setSizeConstraints(superview: self, view: contentView)
    }


    func requireToFail(_ gestureRecognizer: UIPanGestureRecognizer) {
        self.panGestureRecognizer.require(toFail: gestureRecognizer)
    } }

以下是添加约束的方法:

static func setSideConstraints(superview: UIView, view: UIView) {
        let topConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                   attribute: .top,
                                                                   relatedBy: .equal,
                                                                   toItem: superview,
                                                                   attribute: .top,
                                                                   multiplier: 1.0, constant: 0.0)

        let bottomConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                      attribute: .bottom,
                                                                      relatedBy: .equal,
                                                                      toItem: superview,
                                                                      attribute: .bottom,
                                                                      multiplier: 1.0, constant: 0.0)

        let leadingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                       attribute: .leading,
                                                                       relatedBy: .equal,
                                                                       toItem: superview,
                                                                       attribute: .leading,
                                                                       multiplier: 1.0, constant: 0.0)

        let trailingConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                        attribute: .trailing,
                                                                        relatedBy: .equal,
                                                                        toItem: superview,
                                                                        attribute: .trailing,
                                                                        multiplier: 1.0, constant: 0.0)

        superview.addConstraint(topConstraint)
        superview.addConstraint(bottomConstraint)
        superview.addConstraint(leadingConstraint)
        superview.addConstraint(trailingConstraint)
    }

    static func setSizeConstraints(superview: UIView, view: UIView)
    {
        let wConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                 attribute: .width,
                                                                 relatedBy: .equal,
                                                                 toItem: superview,
                                                                 attribute: .width,
                                                                 multiplier: 1.0, constant: 0.0)

        let hConstraint: NSLayoutConstraint = NSLayoutConstraint(item: view,
                                                                 attribute: .height,
                                                                 relatedBy: .equal,
                                                                 toItem: superview,
                                                                 attribute: .height,
                                                                 multiplier: 1.0, constant: 0.0)

        superview.addConstraint(wConstraint)
        superview.addConstraint(hConstraint)
    }

如您所见,我将 contentView 涂成红色,以便在屏幕上定义它。显示我的 PlayerContentView 后,我得到了这个:

PlayerContentView 在全屏上拉伸,所以我希望 contentView 是全尺寸的,但显然不是。有人可以请我参考该问题的解决方案吗?提前致谢!

【问题讨论】:

    标签: swift uiscrollview constraints interface-builder nslayoutconstraint


    【解决方案1】:

    你可以设置

    contentView.translatesAutoresizingMaskIntoConstraints = false
    

    【讨论】:

    • 您好,感谢您的回复!正如您在setupScrollProperties() 方法中看到的那样,我做到了,但它没有改变任何东西:(
    • 你将它设置为 self 而不是 contentView
    • 您是 100% 正确的,先生!您的解决方案确实有帮助,非常感谢!但请注意:我需要将每个子视图 translatesAutoresizingMaskIntoConstraints 设置为 false 以应用约束。
    • 是的,您必须将此属性更新为 false 才能设置约束
    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多