How to update constraints after adding UIView from

2019-09-21 07:44发布

I have a UIViewScroll(background color is blue) in view controller. I need a UIView(background color is white) that were from Xib. The Xib view has a UILabel(background color is green) with constraints. Now, the problem is UILabel constraints not applied after adding it to scrollView. How to add UIView without loss of constraints? Refer following screenshots and code.

Note:

I need to just update constraints of the sub views of the UIView without using IBOutlets of NSLayoutConstraints.

UIView on Xib:

enter image description here

Code:

class ViewController: UIViewController {

    @IBOutlet var scrollView: UIScrollView!

    var profileView:UIView!

    override func viewDidLoad() {
        super.viewDidLoad()


        self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
        self.scrollView.addSubview(self.profileView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.profileView.layer.frame.size = CGSize(width: self.scrollView.frame.width, height: self.profileView.frame.height)
        self.profileView.layer.position = CGPoint(x: self.scrollView.frame.width/2, y: (self.profileView.frame.height/2)+10)
    }

}

Output:

enter image description here

Update: More Information

I am aware to set contentSize of the scrollView. I used layer properties of UIView for manipulating height and width of the UIView. Instead of changing height and width also I need to update constraints of the sub views of UIView.

This is an example for understanding. But, In real I will be add more views like that.

Github Repository : https://github.com/RAJAMOHAN-S/ScrollViewTest

Required output:

enter image description here

1条回答
Fickle 薄情
2楼-- · 2019-09-21 08:19

Your best bet is to set the constraint of self.profileView programmatically, I've added an example below to get you started.

class TestVC: UIViewController {

    @IBOutlet var scrollView: UIScrollView!

    private var profileView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.profileView = UINib.init(nibName: "ProfileView", bundle: nil).instantiate(withOwner: self)[0] as! UIView
        self.configureProfileView()
    }

    private func configureProfileView() -> Void {

        self.profileView.translatesAutoresizingMaskIntoConstraints = false

        self.scrollView.addSubview(self.profileView)
        self.profileView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor).isActive = true
        self.profileView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true

        // Pin the profile view to the top of the scrollView
        self.profileView.topAnchor.constraint(equalTo: self.scrollView.topAnchor).isActive = true
    }
}

More information can be found here too: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ProgrammaticallyCreatingConstraints.html

查看更多
登录 后发表回答