Reputation: 8320
I want to display a button(blue square in below images) at fixed distance from bottom of screen.
Below are contraints :
When running app on different simulators, button is displayed at different positions. Look below images to see the difference.
iOS 6.1 and 4 inch simulator :
iOS 7 and 4 inch simulator :
iOS 6.1 and 3.5 inch simulator :
On iOS 7 and 3.5 inch simulator, this button is not visible.i.e its origin.y
is greater than screen height.
Any idea why this happens?
EDIT : Edited constraints (still not working)
UPDATE : I could narrow down the scope of issue. In storyboard, I have this button and other subviews in main view. Programmatically I add mapView to this main view and add all main view's subviews to mapView as shown below.
// Add mapView to main view
mapView = ...;
[self.view addSubview:self.mapView];
// Remove subviews from main view
[self.button removeFromSuperview];
...
// Add subviews into mapView so that they are visible on map
[self.mapView addSubview:self.button];
...
Now, If I comment above code and run the app then button is displayed at correct position, but with above code uncommented I run into the issue of button misplacement.
UPDATE : Partial answer to this issue is to add constraints programmatically. I said partial, because I have other views, along with this button, which are displayed properly. I don't have their constraints in code. Thus, there is still something to explore.
Upvotes: 2
Views: 2612
Reputation: 781
You are aligning the Button to the bottom layout guide which differs from aligning it to the bottom of the superview which I would suggest in this case. Aligning it to the vertical layout guide depends on iOS6/7 and if you allow the content of the view controller to go beneath the navigation bar / bottom bar.
Aligning it to the superview (container) could solve your issue. But the correct way to solve this would set the correct "extend edges" of the view controller. I am assuming that you enabled "under top bars" and "under bottom bars" on iOS7 that will explain the offsets. Disable them would for example mimic the same behaviour as iOS6 and you would not end up with those different offsets.
It really depends on what you have on your view controller (navbar, bottom bar etc.) and how you set the "edgesForExtendedLayout" of your view controller.
Edit:
Calling
[self.button removeFromSuperview];
will remove not just the button from the view but also all its constraints you set previously! So you have to set the constraints again - manually or avoid removing the button from its superview. I would guess thats why it looks fine in the Interface Builder and get messed up at runtime!
Calling this method removes any constraints that refer to the view you are removing, or that refer to any view in the subtree of the view you are removing.
Upvotes: 2
Reputation: 7474
When you add your button programatically then you need to add
Autolayout constraints programatically as well
Upvotes: 1
Reputation: 4042
Since you are using X-Code 5.0.2
, the xib's
interface builder automatically comes as a Retina 4 inch full screen
by default!!
Try changing that in Attributes Inspector
to Retina 3.5 inch full screen
.
Then change your constraints accordingly.
Try changing this & see how you need to change your constraints for 3.5 inch screen
.
Upvotes: 0