简体   繁体   中英

Programmatically add constraints to UIView or UIViewController?

I'm working without Interface Builder. I have a custom view that extends UIView, and a custom controller that extends UIViewController.

My question is - should I add constraints in UIView.updateConstraints, or in UIViewController.viewDidLoad?

It seems like UIView.updateConstraints is the right place, but then I see a lot of code examples where people to it in UIViewController.viewDidLoad. In addition, if I want to constrain against topLayoutGuide, that's a property on the viewController...so now I have to figure out how to get access to the viewController in the view. Yuck.

But it just doesn't seem right to do layout (view type work) in the viewController. If you were using IB for example, the constraints are added to the view.

Any suggestions?

If the constraints scope are inside the view then place it within updateConstraints .

Also please also explore updating constraints in View controllers

 -(void) updateViewConstraints

method.

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