iOS: dimensions of search bar in navigation controller - ios

I have a UISearchBar in my navigation controller bar title view. I followed the first answer here How to add search bar in navigation bar for iphone?. This works for me but the dimensions of the search bar are hardcoded. How can I set the dimensions so that it looks nice on iPad and iPhone in both portrait and landscape mode?
This is the code in my search bar controller.
self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 310.0, 44)];
self.view.autoresizingMask = 0;
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(-5.0, 0.0, 320.0, 44.0)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate = self;
[self.view addSubview:_searchBar];
Then in my main viewController I have
self.navigationItem.titleView = _searchController.view;

There are many ways to do this, and here are some articles that may be helpful:
Get the device's orientation, then use different sets of values accordingly
Figure out which device the app is running on
Get the dimension of the screen

Related

Why iOS 11Custom UINavigationItem titleView and leftBarButtonItem are varying from iOS9?

My Puzzle is "Why iOS 11Custom UINavigationItem titleView and leftBarButtonItem are varying from iOS9?".
When I customize titleView and leftBarButtonItem in navigationItem of my HomeViewController, I found something different here.
The following code successfully places the custom leftBarButtonItem and custom titleView in the navigation bar.
// customize leftBarButtonItem
_customLeftItemView.frame = CGRectMake(0, 0, 60, 40);
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(clickLeftItemAction:)];
[_customLeftItemView addGestureRecognizer:tapGes];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_customLeftItemView];
leftItem.width = 60.0f;
self.navigationItem.leftBarButtonItem = leftItem;
// customize titleView
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
searchBar.placeholder = #"Please search address";
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
The _customLeftItemView is like the figure below.customLeftItemView In Storyboard
Above the code executed,the appearance difference between iOS11 and iOS9, like the figures below.iPodSimulator iPhone8 iOS11
When I checked the difference through Debug View Hierarchy tool, I found searchBar occupies the entire area of the UINavigationBarContentView and UISearchBarTextField on the right side of the UIButtonBarStackView when App ran in iPhone 8. however when App ran in iPod(iOS 9), searchBar didn't occupy the entire area of the UINavigationBarContentView, it is on the right side of the UIButtonBarStackView. the code _customLeftItemView.frame = CGRectMake(0, 0, 60, 40); is effective in iOS9, so the leftBarButtonItem width is fixed, if run in iOS11, it loses effectiveness. the difference like the figures below.iPod Debug View Hierarchy iPhone8 Debug View Hirarchy
Could you help me with this?
Thanks for your help.

UINavigationBar title text partially appears on screen

I am creating a UINavigationBar programmatically, which is displaying as intended in iOS 7. However, in iOS 8.x all the text in the UINavigationBar doesn't appear within the bounds of the screen. One of my assumptions is this has something to do with Auto Layout.
//ViewControllerRootHomeCenter.m
_navBar = [[UINavigationBar alloc] init];
_navBar.frame = CGRectMake(0,0, self.view.frame.size.width, 64);
// add hambuger menu
//...etc etc
[self.view addSubView:_navbar];
You can try to obtain the screen width this way too:
CGRect frame = (CGRect){0,0,CGRectGetWidth([[UIScreen mainScreen]bounds]),64);
Perhaps it will fix your problem.
First of all, make sure you're not hard-coding the width of the navigation bar.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
Secondly, if your auto layout is enabled, make sure there are no conflicting constraints. If it's not enabled, then I don't see why you should be having this problem.

How to design custom navigation bar

I have searched lots for how to design navigation like given in the image.
I have created category for UINavigationBar like given in the following solution
Custom nav bar styling - iOS
Able to increase the size of NavigationBar using below code.
CGFloat navBarHeight = 84.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
But all content like (left button, right button, title) moves to bottom of bar.
When attach segment control to titleView like below
self.navigationItem.titleView = self.segmentedControl;
All views( left button, title, right button) as liner.
I want segment control bottom of the navigation bar as shown in the image.
#Shankar Shinde why are you adding segment controller as navigation title view you can add below the navigation bar using storyboard: check below screen shot
I think you need to hide the navigation bar using following code and create view like attached screenshot let me know it's help you or not ?
[self.navigationController.navigationBar setHidden:YES];
Please check it out sample:
https://github.com/boctor/idev-recipes/tree/master/WoodUINavigation
It may help you :)
You can use Prompt provided for navigationItem
First give prompt to your navigationItem
self.navigationItem.prompt = #"";
Create a segmentcontroll
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 1;
[self.navigationController.navigationBar addSubview:segmentedControl];

Strange behavior with UIToolbar in iOS app

In my current project I have an app based on a navigation controller. Some of the view in the app require a toolbar at the bottom of the view. I am creating the toolbar programmatically with the following (excerpted from viewDidLoad in a table view controller):
UIBarButtonItem *reloadButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshList)];
[reloadButton setTitle:#"Refresh"];
[reloadButton setTintColor:[UIColor whiteColor]];
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
438,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:reloadButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController setToolbarHidden:NO];
This has worked well in most views. However, I am now incorporating it into another view using identical code. In that controller, the toolbar appears about an inch above the bottom of the view--it doesn't snap to the bottom of the view as it does in other view controllers. Once I have navigated to the problem view, the other toolbars in other view begin exhibiting the same behavior. This is only happening in the simulator, not on my physical device. However, I currently have only an iPhone 4 as my physical device. Is this a bug in the simulator or an indication of an issue somewhere? THanks!
You are setting the y position to a hardcoded value of 438. This will result in it not appearing at the bottom of the screen for the taller iPhone 5's). You should calculate the y position as well perhaps with:
CGRectGetMaxY(self.view.bounds) - toolbarHeight
this would result in:
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
It could also have to do with the presence of a navigation bar and the status bar

Adding updating elements such as a level number and coin total to a navigation bar?

I have managed to change the navigation bar to a custom stretch image and turn the back button a different colour. I'm having trouble setting a level number and coin total in the navigation bar.
I figure there has to be any easier way then messing about with progress HUD's and such - which is all I have seen mention from my research.
I'm trying to achieve a similar look to that attached below - element wise not graphically.
Thank you for any help in advance.
Two solutions:
Make the entire navigation bar into a custom view with your own elements, then update them as needed. Do this using the titleView property of UINavigationItem.
Use the leftBarButtonItem, titleView, and rightBarButtonItem with your own custom views.
I prefer the second method because it is more scalable (visually) - that is your view controller will layout correctly in landscape mode, or on an iPad, or in an oddly sized popover, etc. The left item will align to the left, the right to the right and the middle one in the middle. However, it's a bit more complicated because the left and right items need to be of type UIBarButtonItem. We can get around that like this:
// Set up the red star thing in the middle with a number 6 on it
MyRedStarView* redStarView = [MyRedStarView redStarViewWithValue:6];
self.navigationItem.titleView = redStarView;
// Set up the 'word' button on the left
MyWordButton* wordButton = [MyWordButton defaultWordButton];
UIView* buttonHolderView = [[UIView alloc] initWithFrame:CGRectZero];
buttonHolderView.backgroundColor = [UIColor clearColor];
[buttonHolderView addSubview:wordButton];
buttonHolderView.frame = wordButton.frame;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonHolderView];
// Set up the coin indicator on the right
MyCoinView* coinView = [MyCoinView coinViewWithValue:515];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:coinView];
Notice how I wrapped the button (on the leftBarButtonItem) in a holder view - this is only necessary if you want to do some kind of view transitions on the button. For example if it changes in different contexts and you want to animate the transition by removing the button from the holder view and adding a different one (with a view transition). On the right bar button item I didn't do this, just to show the different approaches.
Of course I used some fake view types to demonstrate - you'd actually have your own references to these so that you can set the property values and update the display of the numbers.
try the following code assuming you already have the navigationView in place
- (void)viewDidLoad
{
// Custom Navigation Bar
// -----------------------------------
UIView *navigationCustomTitle = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 20.0)];
navigationCustomTitle.backgroundColor = [UIColor clearColor];
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
UILabel *titleCustomLabel = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 0.0, 260.0, 20.0)];
titleCustomLabel.text = #"A nice title";
titleCustomLabel.textColor = [UIColor whiteColor];
titleCustomLabel.backgroundColor = [UIColor clearColor];
titleCustomLabel.font = [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:(16.0)];
[navigationCustomTitle addSubview:titleCustomLabel];
[navigationCustomTitle addSubview:icon];
self.navigationItem.titleView = navigationCustomTitle;
}

Resources