Gap between nested container view controllers - ios

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.

Related

How do I add a back button to my UINavigationBar?

A user arrives at this viewController from the previous one through a push segue, so I want there to be a back button in the UINavigationBar to allow them to return.
Normally this back button would appear by default if I right clicked on the viewController in storyboard and selected Embed in > Navigation Controller, but doing this is causing crashes, and I prefer doing things programmatically, so I decided to do it in viewDidLoad like so:
// Nav bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
// Back Button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
[self.view addSubview:navbar];
This successfuly adds a navigation bar up top, but it doesn't add the back button as expected. I've tried the solutions given here, here, and here, but none have solved the problem.
You have constructed a UINavigationBar and a UIBarButtonItem but never connected the two.
UINavigationController manages a UINavigationBar for you and sets it's topItem to be the navigationItem of the topmost view controller in the navigation stack. If you need to build your own navigation bar then you need to manage it's item stack yourself. Take a look at UINavigationBar's - pushNavigationItem:animated: method.
However given that you seem to want the look and functionality of a UINavigationController it is unclear to me why you are adding a UINavigationBar view yourself instead of using that container view controller.

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

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.

size of the tabBarItems in a UITabBarController

I have 4 tabs in my APP for iPad. The problem is that sometimes the text is bigger than the button but i have more space to use. However, the UITabBarController doesn't use all the width for the tabBarItems. Is there an option to do it?
UITabBarController will automatically insert spaces in between. You could do two things
Override UITabButton's sizeThatFits method to give it your own size
Insert negative-width spaces in between
For option 2, here's some sample code I used for toolbars of UINavigationBar (and should be applicable to UITabBarController as well)
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
space.width = -10;
[self setToolbarItems:[[NSArray alloc] initWithObjects:tab_item1,space,tab_item2, nil] animated:NO];
I'd suggest to use one space item to the left, one space item to the right and a space item for between of each tab buttons.

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