Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center a UIView programmatically on top of an existing UIView using Autolayout?

In case of a successful Foursquare checkin, my iPhone app shows adds a view on top of the view which is shown.

I want the view to be centered on the X and Y and for it to have a definite width and height using autolayout, but I'm not sure which constraints to add programmatically. I know how to do it in Storyboard, but I'm not sure what exactly to type in code to do the same as this view is added later in response to a successful action in the app.

I can't get it working properly and the UIView has a nib which it loads with loadNibNamed:.

SuccessView *successfulCheckInView = [[SuccessView alloc] initWithFrame:CGRectZero]; successfulCheckInView.placeNameLabel.text = properVenue.name; successfulCheckInView.placeAddressLabel.text = properVenue.address; successfulCheckInView.delegate = self;  [self.ownerVC.view addSubview:successfulCheckInView]; 
like image 979
Anthony Glyadchenko Avatar asked Aug 28 '13 19:08

Anthony Glyadchenko


People also ask

How do I center an image in Xcode?

Select your image view and apply width and height constraints via the pin dialogue box (screenshot below), then open your alignment constraints and select center horizontally and center vertically. Then just apply your constraints and voila!

What are two properties that auto layout constraints control on a UIView?

Auto Layout defines margins for each view. These margins describe the preferred spacing between the edge of the view and its subviews. You can access the view's margins using either the layoutMargins or layoutMarginsGuide property. The layoutMargins property lets you get and set the margins as a UIEdgeInsets structure.

How do I create a view programmatically in Swift?

Creating UIView ProgrammaticallyviewDidLoad() // Do any additional setup after loading the view, typically from a nib. let myNewView=UIView(frame: CGRect(x: 10, y: 100, width: 300, height: 200)) // Change UIView background colour myNewView. backgroundColor=UIColor. lightGray // Add rounded corners to UIView myNewView.


1 Answers

Try this:

NSLayoutConstraint *xCenterConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]; [superview addConstraint:xCenterConstraint];  NSLayoutConstraint *yCenterConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]; [superview addConstraint:yCenterConstraint]; 

Updated for Swift:

NSLayoutConstraint(item: view1, attribute: .centerX, relatedBy: .equal, toItem: view2, attribute: .centerX, multiplier: 1, constant: 0).isActive = true  NSLayoutConstraint(item: view1, attribute: .centerY, relatedBy: .equal, toItem: view2, attribute: .centerY, multiplier: 1, constant: 0).isActive = true 
like image 54
Eric Avatar answered Sep 20 '22 17:09

Eric