Reputation: 20746
I want to create a UIViewController
that should be separated in two equal (in both width and height) parts.
The top one should contain UIView
. The bottom one should contain the button centered both horizontally and vertically in this part:
I placed the UIView
object to the right place by setting its leading, trailing and vertical space to superview to 0
. I made it 0.5x screen's size by setting the equal heights
for this UIView
and its superview and gave it 2:1 multiplier where the first value related to the superview and the second value to the specified UIView
.
But how can I place the button at the center of the bottom half of the screen?
I'm using auto-layouts and size classes.
Upvotes: 2
Views: 5571
Reputation: 649
Swift 5 with SnapKit
customView.snp.makeConstraints { (m) in
m.centerX.equalToSuperview()
m.centerY.equalToSuperview().multipliedBy(1.5)
}
Upvotes: 0
Reputation: 1885
button.centerX = view.centerX
button.centerY = view.centerY*1.5
will give you what you want. You should add the width and height constraints of button as you demanded.
Upvotes: 2
Reputation: 22374
I'll suggest you to take 2 UIViews instead 1, i.e top view and bottom view.
UIViews
to 0, make equal height. UIView
i.e bottom view set button to center of that view and give center X and center Y constraints along with height and width to button. Upvotes: 2
Reputation: 114
These are the steps you need to do. 1. Go to the size inspector with your UIView selected. For X and Y should equal 0. For example, Width should be 600 and height should be 300. So the whole screen is 600 by 600. 2. Select the button with the size inspector still selected. Observe the width and height values. Realize that X and Y is measured from the top left corner of the button. Now take your width of the whole screen and subtract half the width of your button, also take the height of half your screen and subtract half the height of your button. Enter these values into the position of your buttons x and y values.
i.e. if button is 50X100 (width by height) you need be at x= 300-50/2= and y= 450 -100/2=400
Upvotes: 0
Reputation: 6176
assuming your button superview is directly your viewController:
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
myButton = CGPointMake(myButton.superview.center.x, myButton.superview.frame.size.height * 3 / 4);
}
Upvotes: 1
Reputation: 745
You can use Alignment constraints like Horizontally in container and Vertically in container. Hope this helps.
Upvotes: 0