Reputation: 110
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?
Upvotes: 0
Views: 890
Reputation: 798
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];
Upvotes: 1
Reputation: 333
The problem is that you need to add constraints to self.customActionSheet.view
.
When you load a controller view by pushViewController:animated:
message, the system add the view the constraints needed so it fills all screen space.
Instead, if you add it manually (via addSubview:
message), autoulayouts constrains are not created automatically, and you need to create and add them to the view programmatically.
I post here an example of what you may need so you customActionSheet
fills all screen space:
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.customActionSheet.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.customActionSheet.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.customActionSheet.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0];
NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.view
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.customActionSheet.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0];
[self.view addSubview:self.customActionSheet.view];
[self.view addConstraints:@[left, right, top, bottom]];
Upvotes: 0