Is there a preferred way to snap views to the bottom edge of their superviews for iOS 7 apps?
-
21-12-2019 - |
Question
So I have a collection view that has a filter button I can tap to display a filter picker. My intentions were to have the filter picker display itself at the bottom of the view it was added to. In this case I added it to the controllers view e.g. self.view.
Currently I'm doing this:
CGFloat viewHeight = [[self view] frame].size.height;
CGFloat pickerWidth = [[self view] bounds].size.width;
CGFloat pickerHeight;
UIPickerView *filterPicker = [[UIPickerView alloc] init];
pickerHeight = [filterPicker frame].size.height;
[filterPicker setFrame:CGRectMake(0, viewHeight - pickerHeight, pickerWidth, pickerHeight)];
[[self view] addSubview:filterPicker];
It works fine on both iPhone 4 and 5. How reliable is this? Is there another way I should be doing it?
I did try:
[filterPicker setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin];
But it has no effect what so ever.
Regards
Solution
I guess you would have a problem when your ViewController supports rotation, you would need to re-layout the position of your Picker after the rotation.
I prefer to use autolayout - with Xcode 5, using autolayout in the interface builder works much easier than previously with Xcode 4. If you want to use autolayout in code, have a look at FLKAutolayout which makes this a lot easier.
For example, putting a view at the bottom of its superview would look something like this:
UIView *view = [[UIView alloc] init];
[superview addSubview:view];
[view alignBottomEdgeWithView:superview predicate:@"0"];
[view alignLeading:@"0" trailing:@"0" toView:superview];
[view constrainHeight:@"44"];
This example aligns the view view at the bottom of its superview, fixes its height to 44px and the width to match the width of the superview.