I am trying to add a subview to an existing view. I am basically adding a subview to mimic an ActionSheet like this example:
http://cocoaallocinit.com/2014/03/23/implementing-a-custom-uiactionsheet
This example works fine as long as I do not use autolayout on the custom ActionSheet. When I do I get some problems. The subview I am adding consists of one red view. This red view has constraints for its height, and horizontal and vertical space.
(source: bildr.no)
My problem is that the height does not seem to be correct on 4S and 5S devices. The view gets taller on the 5S than the 4S. I expected it to be the other way around as the 5S has more points than the 4S.
(source: bildr.no)
I add the subview with this code:
[self.view addSubview:self.customActionSheet.view];
[self.customActionSheet viewWillAppear:NO];
If I instead add the view by pushing it:
[self.navigationController pushViewController:self.customActionSheet animated:YES];
Then the result is like I expected. The 4S is completely covered by the red view and the 5S still has some area above the red view.
What am I doing wrong here?
Your view does not auto resize according device screen. [UINavigationController pushViewController: animated:]
will work because navigation controller set view's frame for you. The quick way to do is set your view's frame with current view.
self.customActionSheet.view.frame = self.view.bounds;
And do not invoke viewWillAppear:
yourself, system will invoke it.
My suggestion is use auto layout to fulfil your view to superview. Following code uses PureLayout: https://github.com/smileyborg/PureLayout
[self.view addSubview:self.customActionSheet.view]
[self.customActionSheet.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];