I added a UICollectionViewController using IB. Then I added a UINavigationBar in code:
UINavigationBar *navBar = [[UINavigationBar alloc] init];
[navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
[navBar setDelegate:self];
[self.view addSubview:navBar];
All works fine except the fact that the collection view content is hidden by the navigation bar. It doesn't only scroll beneath the navigation bar but in fact displays beneath it when first loaded.
How can I fix that?
Why are you using a standalone navigation bar? It's probably a bad idea. Embed your view controller in a UINavigationController, and make sure the top contraint of your UICollectionView is set to the top layout guide and not the view.
try changing your content offset of the collection view. I'd try either 44 (height of nav bar) or 64 (height of nav bar + status bar)
Your problem is adding UINavigationBar on self.view which is UICollectionView.
You may use simple UIViewController with UICollectionView as subview of its view. Then after adding UINavigationBar you have to change collectionView.frame.
Related
I am trying to customise the navigation bar with title view.
But it seems setting title view comes with its own left and right and top paddings.I was expecting the title view to cover the whole navigation bar according to the frames given.
Is it expected behaviour and if YES than how to deal with that?
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
view.backgroundColor = [UIColor greenColor];
//Navigation Bar
self.navigationItem.titleView = view;
If you just want the navigation bar to be green use [self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]]; in iOS 7+ or [self.navigationController.navigationBar setTintColor:[UIColor greenColor]]; in iOS 6-
Yes, it seems it is not possible to remove that left/right padding. This is the screenshot form debugging views in Xcode after running your code
The outside view that is gray is the navigationBar and the green is obviously the titleView. No matter the frame for the titleView, it is clipped.
Apple documentation says just this:
Custom title views are centered on the navigation bar and may be resized to fit.
I think the only solution would be to subclass the navigationBar, so that you override the titleView frame.
I am trying to add Navigation Bar to a UITableViewController without using NavigationController.
So in viewDidLoadMethod of MyUITableViewController, I create a Navigation Bar using CGRect.
UINavigationBar * navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 30, 320, 50)];
Then I present MyUITableViewController from MainViewController
[self presentViewController:controller animated:YES completion:Nil];
At this point my MyUITableViewController is overlapping with NavBar
I was thinking of creating tableViewController with initWithFram(x, y, width, height) to compensate for NavBar.
But I was not sure what height I should use and how to come up with a value. Would it have the correct scroll behavior??
What is the right way? Please note at this point I do not want to use Navigation Controller.
Is it possible w/o Navigation Controller.
Try not to use UITableViewController, but use simply UIViewController, add a UITableView object on it, and also implement the 2 protocols for table views.
You can resize the table view as you need, you can set its origin Y coordinate to 44 or 64 if you also need the status bar visible.
You can add a UINavigationBar object on top of it.
I hope doing this will help you solve the problem.
I can't add a view under UINavigationBar (Navigation Controller), like the Facebook app. Any ideas on how I can do this?
Best,
Andrea
The custom view looks like a tableHeaderView. Which means you need to set your view as the tableHeaderView, which will then be placed on top of the tableView and underneath your navigation bar.
UIView *customView = [[UIView alloc] initWithFrame:yourFrame];
self.tableView.tableHeaderView = customView;
I have a UITabBarController and I want to add a UIView as a subview but I want that view to cover the whole screen including the tabs on the bottom. All attempts I have done result in the view cover everything except the tabs on the bottom.
Not sure what you have tried but, if you are trying to add the view from a UIViewController that is inside the UITabBarController then you should use:
UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(0,
0,
self.tabBarController.view.frame.size.width,
self.tabBarController.view.frame.size.height)];
[self.tabBarController.view addSubview:coverView];
//and maybe [self.tabBarController.view bringSubviewToFront:coverView];
I want to make an app, which uses a navigationbar, but just for showing the title and some buttons (it is using a vie container). I want the navigation bar to "fusion" with the status bar, like this
But when I use a generic view controller and drag a navigation bar in it just looks as this:
Is there a way to do this?
I've found a solution for this specific problem. Set the navigation bar's translucent property to NO:
self.navigationController.navigationBar.translucent = NO;
UINavigationController will alter the height of its UINavigationBar to either 44 points or 64 points, depending on a rather strange and undocumented set of constraints. If the UINavigationController detects that the top of its view’s frame is visually contiguous with its UIWindow’s top, then it draws its navigation bar with a height of 64 points. If its view’s top is not contiguous with the UIWindow’s top (even if off by only one point), then it draws its navigation bar in the “traditional” way with a height of 44 points. This logic is performed by UINavigationController even if it is several children down inside the view controller hierarchy of your application. There is no way to prevent this behavior.
Full explanation here.
Not sure whether this will fix your issue, but you can try what i did.
I had the same problem in viewcontroller's right side menu "Simulated Metics" > Top Bar > "Opaque Navigation Bar"
viewcontroller.h file in create iboutlet of the navigation bar
#property (weak, nonatomic) IBOutlet UINavigationBar *navBar;
You can do as followings in viewDidLoad
UIView *addStatusBar = [[UIView alloc] init];
//draw status bar with width of device
addStatusBar.frame = CGRectMake(0, -20, self.view.frame.size.width, 20);
//set the background colour to status bar
addStatusBar.backgroundColor = [UIColor colorWithRed:24.0/255. green:24/255. blue:24.0/255. alpha:1];
[self.view addSubview:addStatusBar];
// add your status bar to your UINavigationBar *navBar which declared in your file .h
[self.navBar addSubview:addStatusBar];
navigationController.navigationBar.clipsToBounds = YES;
In order for UINavigationBar to extend its background under status bar its clipsToBounds must be set to NO (which is the default). Make sure you do not mock around with it.
Solution simple as:-
navigationController.navigationBar.clipsToBounds = NO;
Swift 5
navigationController?.navigationBar.isTranslucent = false