I am trying to add three Bar button items using Flexible Space bar but the changes are not getting reflected in simulator. . I have pinned the navigation bar to bottom, left and right
If you use size class and auto layout, add constraints between your toolbar and the View: leading space = 0, trailing space = 0, bottom space = 0, heigh fixed.
You can add as much toolbar items as you wish until you have no space.
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:undo, done, cancel, nil]];
or
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:undo, done, cancel, nil]];
Related
I have added UISearchBar to UINavigationBar and this works really well, Except the left padding of UISerachBar here.
On Debug I came to know that here back button frame is too large that's why UINavigation bar title view shifted from left too much.
How to deal with that?
I want to achieve something like twitter app where the spacing between back button and search bar is not much.
Thanks in advance!
You can add a custom back button in your navigation bar to override the default back button (in this case the left button):
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"my-back-button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
The space between left button and search controller decreases.
As mentioned earlier by Michele Broggi you can try that.
Else
You can also set the search Bar in place of navigation Title view so that it would be centre aligned to the screen (If you want that design)
I'm using a UIBarButtonItem with this result:
I'm using this code to add it to the navigation
self.doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissWebViewController:)];
[self.navigationItem setRightBarButtonItem:self.doneButton];
What am I'm doing wrong?
Looks like your navigation bar is larger than your screen. Check it out into your Storyboard. Select the navigation bar, you must see a square at the begining and at the end of the element. If not, move it to fit the screen.
As a reference a Navigation bar for an iphone 6 4.7 inch application is 375 Width.
In addition, review if you have set constraints correctly. Review that your navigation bar has "spacing to nearest neighbor" as 0 for top and -16 for left and right.
In my UIViewController I have UIImageView that takes up the full size of the controller (screen) and serves as a background. On top of UIImageView I have a UITableView with a clear background. I set the navigation bar to translucent like this:
self.navigationController.navigationBar.translucent = YES;
UIImageView is underneath the navigation bar as I want it to be. Unfortunately the same happens to UITableView. I want to put UITableView at the bottom of navigation bar, leaving UIImageView underneath. If I set:
[self setEdgesForExtendedLayout:UIRectEdgeBottom]
then UITableView is at the bottom of the navigation bar, but the same happens also to UIImageView. What would be the easiest solution to leave UIImageView underneath the navigation bar and push down the UITableView at the bottom of the navigation bar?
By default in iOS 7 the content extends to the top and bottom of the screen, underneath any navigation bars, tool bars, or tab bars. If you were to set the frame of the table view to be start after the navigation bar, the content of the table view would not scroll beneath the navigation bar providing the nice blur effect.
What you'll probably want to do instead, is keep the y origin of your table view at 0, underneath the navigation bar, and set the content inset so the content starts after the navigation bar.
This is pretty simple. If you're using auto layout the top layout guide of the view controller will recognise the height of the status bar and navigation bar so you don't need to calculate this yourself. The bottom of the navigation bar should end at 66.
//Using Auto Layout
CGFloat navigationBarHeight = self.topLayoutGuide.length;
//Not using Auto Layout
UINavigationBar *nav = self.navigationController.navigationBar;
CGFloat navigationBarHeight = nav.frame.origin.y + nav.frame.size.height;
myTableView.contentInset = UIEdgeInsetsMake(navigationBarHeight, 0, 0, 0)
And of course if you actually do want the frame to start after the navigation bar the height above stays the same, you just need to manually set the frame of the table view.
CGRect tableViewFrame = self.view.bounds;
tableViewFrame.origin = CGPointMake(tableViewFrame.origin.x, navigationBarHeight);
tableViewFrame.size = CGSizeMake(tableViewFrame.size.width, tableViewFrame.size.height - navigationBarHeight*2);
myTableView.frame = tableViewFrame;
EDIT: Ah, almost forgot. You'll also want to change the scrollIndicatorInsets to match the contentInset, so your scroll bars don't move offscreen.
myTableView.scrollIndicatorInsets = myTableView.contentInset;
All my buttons in a NavigationItem are set in code for a specific view in the viewHierachy. The title is set with a titleLabel to set minimumScaleFactor and contentCompression to it:
titleLabel.text = self.bookTitel;
titleLabel.minimumScaleFactor = 0.5;
[titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
[titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisVertical];
self.navigationItem.titleView = titleLabel;
Now when the bookTitle is too long it is shortened with ..., but the backButtons title disappears too.
UIBarButtonItem doesn't respond to setContentCompression: so I can't set it to requiredPriority.
How can I prevent the backButton from not showing?
The following is an extract from the Overview section of the UINavigationItem class reference:
The navigation item must provide a title to display when the view
controller is topmost on the navigation stack. In addition, the item
may contain additional buttons to display on the right side of the
navigation bar. You can specify buttons and views to display on the
left side of the toolbar using the leftBarButtonItems property but the
navigation controller displays those buttons only if there is space
available.
If there isn't enough space, the navigation controller won't display any buttons on the left hand side, where your back button is. That's up to the navigation controller...
To prevent the backButton from not showing:
Can you restrict the size of titleLabel further? (The size of the view).
I currently have a button image for the leftBarButton of the UINavigationBar. Is there a way to eliminate the space between this button and the left edge of the UINavigationBar? I thought about just making a custom image for the background of the UINavigationBar, but I want to be able to use the back buttons that are generated on other screens. Ideas?
The layout logic of the navigation bar itself is going to want to try to maintain that space on the left side.
You could try specifying a custom view when setting the navigation items (UIBarButtonItem initWithCustomView:), and pass in a view with a negative x origin -- but I assume the navigation bar would ignore any such origin. (You could also try to keep a reference to that view, and move it to the left after the navigation bar finishes its layout)
Another option would be to try to create your own custom navigation bar -- possibly by putting a custom view in the center of the navigation bar, and stretching it to cover the width & height of the bar.
Try this, it works for me:
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.x = -10;
self.navigationController.navigationBar.frame = frame;
Basically, set the navigation bar x coordinate to negative not the bar item.
in ios7 you can just add a dummy barbuttonitem
for fixing left space you should add dummy as first, for right as last
example for left, you should add this after setting your original items or in viewdidload if you are setting buttons using storyboard.
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *spacerItem = [[UIBarButtonItem alloc] init];
[buttons addObject:spacerItem];
for(UIBarButtonItem *item in self.leftBarButtonItems){
[buttons addObject:item];
}
[self setLeftBarButtonItems:[NSArray arrayWithArray:buttons] animated:NO];