size of the tabBarItems in a UITabBarController - ios

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.

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.

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

How to align UIBarButtonItem in UIToolbar in iOS universal app?

I want to put two UIBarButtonItem instances to UIToolbar in my iOS universal app, on which the first item is on the left-edge of the toolbar and the second is on the center. However, it looks like this cannot be done in Interface Builder, because the auto layout doesn't support forcing constraints on UIBarButtonItem class due to the fact that it is not a subclass from UIView.
Notice that I don't want to put the first on the left-edge and the second is on the right-edge - I have to put the second button at the center. Also, I don't have a plan to use the third button in order to align them as left, center, and right at equal interval.
Is it still possible to add such constraint in this situation in storyboard? I use Xcode 6 beta 5.
You can do it all in your Storyboard.
Select a Bar Button Item in the Object library and drag it into your Toolbar. Add a Flexible Space Bar Button Item at its right. Add your second Bar Button Item at its right. Then, add a second Flexible Space Bar Button Item at its right.
The result will look like this in Interface Builder:
If necessary, you can add an extra Fixed Space Bar Button Item at the right edge of your Toolbar to be sure that your second Bar Button Item is really centered.
User lxt gives another example of this in answer for a similar question here.
Edit: Be sure that your UIToolbar has good constraints before proceeding!
you can do this programmatically
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"item1" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"item2" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithTitle:#"item3" style:UIBarButtonItemStylePlain target:self action:nil];
self.toolbarItems = [NSArray arrayWithObjects: item1, flexible, item2, flexible, item3, nil];
check this link if you want more details
How to place UIBarButtonItem on the right side of a UIToolbar?
I think you should try Flexible bar button space item and for more understating i am sharing a link, so have look of it might this help you out
UIToolbar and Flexable/Fixed bar button items
Code to add toolbar :
Here fixed width can be variable. Accordingly you can keep it as far as you want so as to keep the second button in centre.
UIBarButtonItem *fixedItemSpaceWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedItemSpaceWidth.width = 200.0f; // or whatever you want
UIBarButtonItem *doneBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonAction:)];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelButtonAction:)];
// Initialise toolabr
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-44, 320, 44)];
//toolbar.barStyle = UIBarStyleBlack;
toolbar.items = [NSArray arrayWithObjects:cancelBarButton,fixedItemSpaceWidth,doneBarButton, nil];
[self.view addSubview:toolbar];

How to add UINavigationBar with two UIBarButtonItem programmatically?

I added code to the viewDidLoad method of ViewController. I have two UIBarButtonItem, one of them is on the left called Left and the other one is at the right called Right. I can get the left button to work but I cannot even see the right button. What am I doing wrong?
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 420, 44)];
[navbar setBackgroundImage:[UIImage imageNamed:#"bg_fade.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"Tray Tracker"];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:#"Left" style:UIBarButtonItemStylePlain target:self action:
#selector(leftAction:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Right" style:UIBarButtonItemStylePlain target:self action:
#selector(saveAction:)];
item.leftBarButtonItem = leftButton;
item.rightBarButtonItem = rightButton;
navbar.items = #[item];
[self.view addSubview:navbar];
I do have the IBActions for leftAction and saveAction
If you simply want a bar with two buttons, you should use a UIToolbar instead. It's much easier to handle for this simple case. The only change you would need is to create a UIBarButtonItem flexibleSpace (it's a system item), and call [toolbar setItems:#[leftButton, flexibleSpace, rightButton]];
If you need actual navigation functionality such as a back button, take a look at this answer. You need to use UINavigationBar pushNavigationItem.
using UINavigationBar without UINavigationController
In your example, you are misusing the items property. The documentation for UINavigationBar states,
The bottom item is at index 0, the back item is at index n-2,
and the top item is at index n-1, where n is the number of items in the array.
In order to do accomplish what you're trying to do with a UINavigationBar, I think you need multiple navigationItems.
It would be MUCH easier to either use a UIToolbar or use the UINavigationBar in conjunction with a UINavigationController.
Edit
Now that you posted the image, I think the reason you can't see the right bar button is because your frame width is too large. The iphone screen is 320 px wide, so your frame for the navigationBar should be CGRectMake(0, 0, 320, 44)

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.

Resources