nslayoutconstraint

How to resize a parent view based on the size of subviews layouted with Autolayout

青春壹個敷衍的年華 提交于 2019-11-27 10:22:47
问题 I am preparing a custom view which looks similar to the example shown in the screenshot. I add all user interface components (labels, text fields, radio buttons, ..) at runtime. To position the elements I use Autolayout constraints. This part works fine so far. The number of elements varies from context to context. I wonder how I could use Autolayout constraints to dynamically resize the parent view (most likely the height of the view). The view constraints therefore should consider the

Understanding multiplier in auto layout to use relative positioning

好久不见. 提交于 2019-11-27 09:52:00
问题 I am trying to understand how one can utilize Auto Layout to position items relative to other views percentage-wise. For example, I recently learned that you can specify a view's bottom should lie 4% higher than its superview's bottom by using this: self.view.addConstraint(NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 0.96, constant: 0)) This makes sense to me because a multiplier of 1 would align it right at the view

Using autolayout in a tableHeaderView

纵然是瞬间 提交于 2019-11-27 07:29:34
I have a UIView subclass that contains a multi-line UILabel . This view uses autolayout. I would like to set this view as the tableHeaderView of a UITableView ( not a section header). The height of this header will depend on the text of the label, which in turn depends on the width of the device. The sort of scenario autolayout should be great at. I have found and attempted many many solutions to get this working, but to no avail. Some of the things I've tried: setting a preferredMaxLayoutWidth on each label during layoutSubviews defining an intrinsicContentSize attempting to figure out the

How do I animate a NSLayoutConstraint in Swift?

天大地大妈咪最大 提交于 2019-11-27 06:40:07
问题 I would like to animate an UIImageView. I declared a NSLayoutConstraint in viewDidLoad and used this code: UIView.animate(withDuration: 1) { myConstraint.constant = 100 self.view.layoutIfNeeded() } Why doesn't my image move? 回答1: By the time you hit viewDidLoad , the constraints engine has not yet been applied and the starting location of the views has not yet been established. So, feel free to add the original constraints in viewDidLoad , but you will want to defer the animateWithDuration

Why am I getting a “Auto Layout still required after executing -layoutSubviews” error every time my app launches now?

亡梦爱人 提交于 2019-11-27 06:36:55
问题 Since I added the following code, every time my app opens this UITableViewController it crashes: self.noArticlesView = [[UIView alloc] init]; self.noArticlesView.translatesAutoresizingMaskIntoConstraints = NO; self.noArticlesView.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1]; [self.view addSubview:self.noArticlesView]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.noArticlesView attribute:NSLayoutAttributeTop relatedBy

With Auto Layout, how do I make a UIImageView's size dynamic depending on the image?

冷暖自知 提交于 2019-11-27 06:33:32
I want my UIImageView to grow or shrink depending on the size of what the actual image it's displaying is. But I want it to stay vertically centered and 10pts from the leading edge of the superview. However, if I set these two constraints it complains I haven't set up enough constraints to satisfy Auto Layout. To me it seems to perfectly describe what I want. What am I missing? John Grant The image view's intrinsic size is already dependent on the size of the image. Your assumptions (and constraints are correct). However, if you've set up your image view in interface builder and have not

Programmatically Add CenterX/CenterY Constraints

[亡魂溺海] 提交于 2019-11-27 06:22:30
I have a UITableViewController that doesn't display any sections if there is nothing to show. I've added a label to indicate to the user that there is nothing to display with this code: label = UILabel(frame: CGRectMake(20, 20, 250, 100)) label.text = "Nothing to show" self.tableView.addSubview(label) But now, I want it to be centered Horizontally and Vertically. Normally, I would choose the two options highlighted (plus ones for height and width) in the screenshot: I've tried the following code to add the constraints but the app crashes with error: label = UILabel(frame: CGRectMake(20, 20,

UICollectionViewCell dynamic height with autolayout

[亡魂溺海] 提交于 2019-11-27 05:58:30
问题 I am using auto layout with UICollectionViewCell. So the idea is to allow CollectionViewCell to determine it's size based on layouts. All the constraints are set properly but the problem is that I can not calculate it's size for data source method collectionView:layout:sizeForItemAtIndexPath: Ideally I would like to calculate Cell's height doing the following: static MyCell *myCell = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ myCell = [[MyCell alloc] initWithFrame

Autolayout: origin and size should change according to width and height factor

半世苍凉 提交于 2019-11-27 05:52:44
问题 Here is a scenario what I needed. I have put a UIButton on IB (size iPhone4 inch) whose initial frame on IB is x:100,y:100,w:100,h:100 . I want the origins and height should change according to device size. for example the width and height of iPhone6 is 375X667, hence the width factor (375/320=1.1718) and height factor (667/568=1.1742) . In this scene I want to change my buttons origin X and width with multiply by width factor (1.1718) and origin Y and height with multiply by height factor (1

Programmatically creating constraints bound to view controller margins

这一生的挚爱 提交于 2019-11-27 05:45:38
问题 I'm trying to make a view that will act as a sort of "panel", attached to the right side of the view controller. That is, it is bound to the trailing, top, and bottom margins of the parent view controller, with a static width of 300 However, I just can't seem to get it right, I'm either breaking a constraint or doing something xcode tells me is illegal. What am I doing wrong? Here is the code in the controller let myView = UIView() view.backgroundColor = UIColor.redColor() self.view