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.
Related
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!
I am trying to push a view controller and set its navigation bar's title but it is not centered due to long title being used i guess. Along with that, scope of the back button is increased till the title view, i.e if I tap with with Milestone's "M", it gets back though it frame is the same.
Bounds of back button are the same but its click impact is elongated.
Below is the code for how I am pushing the view controller.
MilestoneDetailsViewController *controller = [[MilestoneDetailsViewController alloc] initWithNibName:#"MilestoneDetailsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
and in the MilestoneDetailsViewController, i set the title as following:
self.title = NSLocalizedString(#"Milestone Details", nil);
Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Try this
UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 160 ,44)];
UILabel *bartitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, tView.frame.size.width, 40)];
[bartitleLabel setBackgroundColor:[UIColor clearColor]];
[bartitleLabel setTextAlignment:NSTextAlignmentCenter];
[bartitleLabel setAdjustsFontSizeToFitWidth:YES];
[bartitleLabel setText:NSLocalizedString(#"Milestone Details!",#"")];
[self.navigationItem setTitle:#""];
[tView addSubview:bartitleLabel];
[tView setBackgroundColor:[UIColor clearColor]];
[self.navigationItem setTitleView:tView];
Try like this
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentCenter;
lbNavTitle.text = NSLocalizedString(#"Milestone Details!",#"");
self.title = lbNavTitle // here self.title is your navigation bar title
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.
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.
I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.
"i don't have navigation controller or need it. i just want to add navigation bar in only one view."
Bellow is my code which i write for adding navigation bar in ViewDidLoad()
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
Thanks in advance....
I used the Interface Builder to make a static tableView in a UITableViewController.
This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:
//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:#"Sign-In"];
//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Zurück" style:UIBarButtonItemStyleDone target:self action:#selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:#"Fertig" style:UIBarButtonItemStylePlain target:self action:#selector(signInDonePressed:)];
//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];
//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];
// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];
// Adding the NavigationBar to the TableView
[self.tableView setTableHeaderView:headerView];
I hope this helps somebody!
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
[bi1 release];
You can add buttons to navigation bar as follows:
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
navBar.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
navBar.leftBarButtonItem = btnCancel;
[btnCancel release];