I want to change the height of a UIViewController's
view that is inside a UINavigationController
to display a banner at the bottom so that it doesn't obscure anything.
I thought this would be pretty easy by just changing the view's frame in the viewDidLoad
but that didn't work:
CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height - 49.0f);
I also tried to add
[navigationController.view setAutoresizesSubviews:NO];
after initiating the UINavigationController
but it still looks the same.
The only option I can think of right now is to use the UINavigationController
inside a dummy UITabBarController
that will be obscured by the banner but that seems unnecessarily complicated to me.
Is there any way to change the height of the view controller's view inside a UINavigationController
?
@MainActor class UINavigationController : UIViewController A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.
To hide or show the navigation bar, use the is Navigation Bar Hidden property or set Navigation Bar Hidden(_: animated:) method. A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigation Item class) associated with the view controllers on the navigation stack.
The navigation controller pushes the specified view controller onto its navigation stack. The navigation controller presents the specified view controller modally. The behaviors of other segue types are unchanged. A navigation controller supports the following behaviors for its interface:
Users can quickly switch between different stack levels with a tap and hold on the back button. The sample shows this by pushing ten view controllers on the current navigation stack to demonstrate that back button titles can be customized for each view controller level in the stack.
There's no way to change a view controller's view from within the view controller, but you could use a custom container view controller:
// Create container
UIViewController* container = [[UIViewController alloc] init];
// Create your view controller
UIViewController* myVc = [[MyViewController alloc] init];
// Add it as a child view controller
[container addChildViewController:myVc];
[container.view addSubview:myVc.view];
myVc.view.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth | UIViewAutoresizingMaskFlexibleHeight;
myVc.view.frame = CGRectMake(0, 0, container.view.bounds.size.width, container.view.bounds.size.height-200);
// Add your banner
UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner"]];
imgView.autoresizingMask = UIViewAutoresizingMaskFlexibleWidth| UIViewAutoresizingMaskFlexibleTopMargin;
imgView.frame = CGRectMake(0, container.view.bounds.size.height-200, container.view.bounds.size.width, 200);
[myVc.view addSubview:imgView];
Now you can add the container
view controller to your navigation controller instead of your one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With