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

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.

Related

How do I fit a UISearchBar in a UIBarButtonItem?

I'm using a UISearchController in iOS 8 and when I make its UISearchBar my table's header view everything is fine. However now I need it in my UINavigationBar's left bar button (can't be the title view because of the vertical centering when I enlarge the nav bar). When I wrap the UISearchBar in a UIBarButtonItem it is larger than the width of the screen.
I think it is related to my view controller initialized from a storyboard with size classes enabled which means my frames are not set till viewDidLayoutSubviews. However I had the same issue with my segmented control in a toolbar and I just called sizeToFit on the toolbar in viewDidLayoutSubviews and that fixed the toolbar. When I do the same for the search bar it still draws partly off screen. Apple does this in some of their iPad apps but how?
Note: I can hack it to get it all on screen but I have to wrap it in another view but then the color is off and it just seems wrong and I think the animation is off.
You need to create a UIBarButtonItem with UIBarButtonSystemItemFixedSpace with -10 width which will remove 10 px padding.
UIBarButtonItem *Spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[Spacer setWidth:-10];
UISearchBar *searchBar= [[UISearchBar alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width-10, 39)];
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:Spacer,searchBarItem,nil];

Navigation bar title is not centred in iPhone's portrait mode

I have created a ViewController Using Xib file of size wAny and hAny and i have used autolayaout and constraint to fit UI all devices and all orientation.
Everything is working fine but when i'm running app on iPhone portrait mode title of navigation bar is not centred where as when rotating it to landscape mode title is centred.
I have used following code for setting title
self.title = #"Download Schedule";
Already tried:
I have tried to set custom UILabel of lesser font size to Navigation bar's title view but still i'm getting it on left side .
Here is code of my right button
//add right bar button for more options
UIView *navBarButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:5];
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[moreButton setBackgroundImage:[UIImage imageNamed:#"ic_web"] forState:UIControlStateNormal];
[moreButton addTarget:self action:#selector(addNewSchedule) forControlEvents:UIControlEventTouchDown];
[navBarButtonsView addSubview:moreButton];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:navBarButtonsView];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item0, backButton, nil];
There is something else going on - something you have not told us about. I suspect that there is something wrong with how you are setting your rightButtonItems, such that there are invisible buttons taking up the space on the right. I can reproduce this issue only by deliberately making the right button really wide:
So I suspect you have applied a large width to the right button, or there are extra invisible right button items taking up the space on the right.

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.

searchbar changes color when in navigation bar

I added a search bar in the titleview of a navigationitem. Searching works properly, but when using the navigation controller, the background of the searchbar changes colors, as seen below
The only place that I touch color is to change the color of the navigation item in the storyboard via xcode
the relevant code for putting the searchbar in the titleview
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
[_searchController.searchBar sizeToFit];
self.navigationItem.titleView = _searchController.searchBar;
What is causing this coloration?
trying to change the bartint color with
[_searchController.searchBar setBarTintColor:[UIColor clearColor]];
but I am getting this
also things that aren't working are
[_searchController.searchBar setBarTintColor:self.navigationController.navigationBar.tintColor];
_searchController.searchBar.backgroundColor = self.navigationController.navigationBar.tintColor;
and with backgroundColor
it seems that this color is somehow other than the settings. See here with redColor
Need to set UISearchBar tint color.
UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, 100, 20)];
[search setBarTintColor:[UIColor clearColor]];
search.backgroundImage=[UIImage new];
self.navigationItem.titleView=search;
Now navigation bar looks like below image :
For Swift Users , just add these two lines :-
searchBar.barTintColor = UIColor.clearColor()
searchBar.backgroundImage = UIImage()
self.navigationItem.titleView = searchBar

iOS: dimensions of search bar in navigation controller

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

Resources