KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
How do you programmatically set a UIView to be in the center of its superview using Auto Layout? UIButton* viewObj = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [viewObj setTranslatesAutoresizingMaskIntoConstraints:NO]; [viewObj setBackgroundColor:[UIColor greenColor]]; [self.view addSubview:viewObj]; NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:viewObj attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [self.view addConstraint:cn]; cn = [NSLayoutConstraint constraintWithItem:viewObj attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; [self.view addConstraint:cn]; The above code works for me for UIButton, but I'm having trouble replacing the first line with something that works for a UIView. I've tried UIView* viewObj = [UIView alloc] initWithFrame:CGRectZero]; but the view does not show up in simulator. Any suggestions? Thanks!
Tags (comma-separated)
Save Edits
Cancel