在awakeFromNib()方法中添加约束

时间:2019-04-01 13:02:11

标签: swift uiscrollview constraints interface-builder nslayoutconstraint

我的项目中有两个自定义视图,需要按完全相同的比例进行缩放。因此,我决定为此使用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之后,我得到了:

enter image description here PlayerContentView在全屏上拉伸,因此我希望contentView处于全尺寸,但显然不是。有人可以请我介绍该问题的解决方案吗?预先感谢!

1 个答案:

答案 0 :(得分:1)

可以设置

contentView.translatesAutoresizingMaskIntoConstraints = false