iOS 10 `rightBarButtonItem` moves outside of navigation bar - ios

In my view controller, I display the search controller in the navigation bar.
/*set appearance of search controller */
_searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
_searchController.searchBar.delegate = self;
_searchController.searchResultsUpdater = self;
[_searchController.searchBar sizeToFit];
_searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
I also created a sort button with the help of the rightBarButtonItem.
/*add drop down sort menu button to navigation bar */
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.titleView = self.searchController.searchBar;
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(displaySortMenu:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
rightButtonItem.image = [UIImage imageNamed:#"Sort"];
self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(6, 0, 0, 0);
In iOS 11, the sort button sits right next to the search controller inside the navigation bar, like so:
However, in iOS 10, the buttons moves completely outside of the navigation bar:
My first thought was to constrain the button in willLayoutSubviews, but as there is no view component of the button, I couldn't set translatesAutoresizingMaskIntoConstraints to NO. I then checked if it was an issue of placing the search controller inside the navigation bar by removing the search controller completely. Still (in iOS 10) the button stays outside of the navigation bar:
How can I fix the rightBarButtonItem to inside the navigation bar for iOS 10 and earlier ?

I needed to release the original button I created for it to correctly stay in the navigation bar as a bar button item.
[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.titleView = self.searchController.searchBar;
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(displaySortMenu:)];
rightButtonItem.image = [UIImage imageNamed:#"Sort"];
self.navigationItem.rightBarButtonItem = rightButtonItem;
[rightButtonItem release]; // <-- this is the new line!

Related

How do I hide that line underneath the UINavBar?

This is the code I am using in viewWillAppear:
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 1420, 50)];
//do something like background color, title, etc you self
[[UINavigationBar appearance] setBarTintColor:[UIColor underPageBackgroundColor]];
self.navigationController.navigationBar.translucent = NO;
[self.view addSubview:navbar];
UINavigationItem *item = [[UINavigationItem alloc]
init];
navbar.items= #[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
I want to remove the shadowImage. The line underneath the nav bar but ONLY in one view controller. How do I do this?
I've already tried this but it changes all view controllers, to be like, invisible:
[[UINavigationBar appearance] setBackgroundImage: [UIImage new]
forBarMetrics: UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage new];
Any help would be greatly appreciated!
I had an issue with white space appearing under my nav bar. Is this what you mean by a line? I solved it within xCode's Interface Builder by:
Selecting the view controller in the storyboard
Going to the attributes section of the right toolbar
Check the box "Under Opaque Bars" in the "Extend Edges" section.
I also added the following code in my viewDidLoad method:
self.automaticallyAdjustsScrollViewInsets = NO;

UICollectionView Top Bar Doesn't Appear In Simulator

I had a little problem which is my uicollectionviewcontroller top bar doesn't appear in simulator and device. I already set the top bar to translucent navigation bar for uicollectionviewcontroller, then I dragged navigation item to navigation bar and finally I set an image for left bar button items to function as back button. Below is there screenshot.
Top Bar Storyboard:
Simulator Result:
It doesn't look like you have a navigation controller, to add a navigation bar programatically without a navigation controller controlling the view hierarchy you can do the following.
-(void) viewWillAppear:(BOOL)animated {
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];
[self.view addSubview: navBar];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navItem.rightBarButtonItem = doneItem;
navItem.leftBarButtonItem = cancelItem;
navBar.items = [NSArray arrayWithObjects: navItem,nil];
[UIBarButtonItem appearance].tintColor = [UIColor blueColor];
}
To add the navigation controller (which will add the bar), highlight the UICollectionViewController in storyboard, and select Editor->Embed in->Navigation Controller from the menus.

Do I need to embed a UINavigationController & Programmatically add UINavigationBar?

I don't see all of my navigation bar.
There is only a outline of where a nav bar would be.
I set the navigationBar Color and tint colors so as to define this nav bar even if it had no items.
I used Storyboards to make the currently functional UI.
My Story Board UI includes Delegates,Custom Collection View Cells,Custom Table View Cells,And Animations.
By the end of my app I wanted to add nav bars programmatically to each of my ViewControllers. (see code)
I have used this code in 6 other classes. And I Have 6 Perfectly Flawless navigation bars in those ViewControllers.
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]
initWithTarget:self
action:#selector(titleGestureLabelMethod)];
UILabel * titleView = [UILabel new];
titleView.text = #"Title";
titleView.textColor = [UIColor greenColor];
[titleView sizeToFit];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:tapGesture];
self.navigationItem.titleView = titleView;
[self.view addSubview:titleView];
[self.navigationController.navigationBar setTranslucent:NO];
UIBarButtonItem *lButton = [[UIBarButtonItem alloc] initWithTitle:#"L"
style:UIBarButtonItemStylePlain
target:sel
action:#selector(lButtonMethod)];
[self.navigationItem setLeftBarButtonItem:lButton animated:YES];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithTitle:#"R"
style:UIBarButtonItemStylePlain
target:self
action:#selector(rButtonMethod)];
[self.navigationItem setRightBarButtonItem:rButton animated:YES];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.view addSubview:testTwoNavBar];
}
You need to embed your view controller into a navigation controller. If you're using Interface Builder, from your storyboard select the view controller. In the menu select editor and choose embed in navigation controller. It's hard to understand what you really need though from this question.

How to add UIBarButton Programmatically to Programmatically created NavBar?

I am changing my view controller using the following code:
[self.slidingViewController performSegueWithIdentifier:#"FindScoresController" sender:self.slidingViewController];
The segue is PUSH
Now I want to add UINavigationBar Programatically. The code i am using for it is:
// Not required if viewController is pushed from previous viewController
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
UIColor *barColor = [[UIColor alloc]initWithRed:82/255.0f green:167/255.0f blue:192/255.0f alpha:1.0f];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 or earlier
navbar.tintColor = barColor;
} else {
// iOS 7.0 or later
navbar.barTintColor = barColor;
navbar.translucent = NO;
}
[self.view addSubview:navbar];
It adds nav bar successfully.
Now I want to add left and right custom buttons with custom Image.
But I am unable to do so.
Code i am using to add right UIBarButton item:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:#"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
But it doesn't show any effect.
I also tried this code:
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
self.navigationItem.rightBarButtonItem = item;
All these code i am writing in ViewDidLoad method.

Add default navigation bar on viewController

I have a button and when pressed it presents a new viewController, like a pop up window.
- (IBAction)btnPressed:(id)sender
{
PopUpObj = [[PopUpViewController alloc] init];
[self.navigationController presentViewController:PopUpObj animated:YES completion:nil];
}
The View appears as it should but it has no nav bar with back button.
In the PopUpViewController I create a nav bar like this:
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor whiteColor];
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
[navbar setBarStyle:UIBarStyleDefault];
[self.view addSubview:navbar];
UIBarButtonItem* myBackButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = myBackButton;
[super viewDidLoad];
}
The nav bar appears but the back button item doesn't appear at all. What's wrong?
The backBarButtonItem is displayed when you push another viewController onto the navigation stack. Think of it as the button that will be used to get back to your PopUpViewController, not the button displayed whilst showing your PopUpViewController. Change self.navigationItem.backBarButtonItem to self.navigationItem.leftBarButtonItem.

Resources