简体   繁体   中英

Modify height of a container view

Here is my app with the profile on the left (ProfileViewController) and a containerView on the right (ContainerViewController). What I am trying to achieve is once the containerViewController has done its job, it will update the height of the UIView.

在此处输入图片说明

// ContainerViewController.swift
let heightConstraint = self.view.constraints.filter { $0.identifier == "Height" }.first
heightConstraint?.constant = height

When browsing the list of constraints, they're all emtpy, and I did set up some constraints in the storyboard. So Why ? Is there any way to access the UIView within the ContainerViewController ?

Ps:

DispatchQueue.main.async {
  self.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
}

Sounds to work, but I think that modify constraint is proper ? Nope ?

What I would do, is to set IBOutlet for the constraint, and store it inside ContainerView.

This way, you don't depend on order number or anything else, to get this constraint, even if code (or storyboard) will changed in the future.

And Yes, the right way is to set the constraint constant, and not changing frame. But be aware, that even constraint constant change need to be put on the main (UI) thread - (by the look of your code, I assume, you are dealing with the threads).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM