'Done' UINavigationBarButtonItem on right side cut off - ios

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.

Related

Gap between nested container view controllers

Have a UITabbarController and two UINavigationViewController nested in. UINavigationViewControllers have toolbars both, but toolbars are not laying at the bottom, but 44px upper. Why?
Toolbars added programmatically:
UIBarButtonItem *update = [[UIBarButtonItem alloc] initWithImage:[PfbUtility imageFromConfigIfExist:#"reload"] style:UIBarButtonItemStylePlain target:self action:#selector(eah)];
self.toolbarItems = [NSArray arrayWithObjects: update, nil];
self.navigationController.toolbarHidden = NO;
Attach the bottom side of the splitview to the bottom edge of the screen/view (below the tabBar), not to the top of the tabBar. The application understands that it is below a tabBar and will automatically offset/inset content based on this. This is not a 'nasty fix', it is the intended way to do it. When doing this, you also have the option of using translucent navigation and tabBars.

Resizing UISearchBar in UINavigation Bar?

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)

Adding more than two Bar Button Items to a Navigation Bar

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]];

Position Toolbar on top of the screen with correct shadow in the bottom

I have navigation controller based app and one viewcontroller presents modally graph in a landscape mode. I then add Toolbar with Done button to dismiss the graph vc and return to navigation and portrait mode.
I can't figure out how to position the Toolbar on top of the graph viewcontroller with correct shadow on the bottom of the toolbar. So far I have this code to add the toolbar to the bottom position, which has default shadow on the top of the toolbar. Is it allowed to have toolbar on top of the screen? For the reason of forced orientation rotation I cannot use navigation controller with the graph vc. Platform is iOS7 and iPhone only. Thanks.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - 44.0, self.view.bounds.size.height, 44.0)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
toolbar.items = [NSArray arrayWithObjects:flexibleSpaceButtonItem, doneButtonItem, nil];
[self.view addSubview:toolbar];
I think your frame is looks a bit strange. You are calculating the y position from the view width and the width from the view height.
Maybe you have to specify that the toolbar is on top using the UIBarPositioning protocol.
UIImage *shadow = [toolbar shadowImageForToolbarPosition: UIBarPositionAny];
[toolbar setShadowImage:shadow forToolbarPosition:UIBarPositionTopAttached];
Next Edit:
This is what the documentation has to say about the iOS 7 UIToolbar:
UIBarPositionTop
Specifies that the bar is at the top of its containing view.
The system uses this as a hint to draw directional decoration accordingly. For example, any shadow would be drawn below the bar.
Instances of UIToolbar do not appear with this position on iPhone, but they can on iPad.
Available in iOS 7.0 and later.
Declared in UIBarCommon.h.
Maybe toolbars are not meant to be used on top. However, you can simply add a shadow with addSubview:
Try to implement the method
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
from of UIToolbarDelegate protocol.

Eliminating the space on the UINavigationBar between the left of the bar and the leftBarButtonItem

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];

Resources