Adding UIToolbar programmatically - ios

What am I doing wrong here ? I just don't see the toolbar at the bottom of screen Here's my code.
CGRect rect = self.view.frame;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(rect.origin.x,
rect.size.height-44,
rect.size.width,
44)];
self.bottomToolbar = toolBar;
[toolBar release];
[_bottomToolbar setBackgroundImage:nil
forToolbarPosition:UIToolbarPositionBottom
barMetrics:UIBarMetricsDefault];
[self.view addSubview:_bottomToolbar];

You need to set the toolbar's autosizingMask to the "flexible top margin" value.
Also, your code deals with the toolBar variable, the bottomToolbar property, and the _bottomToolbar ivar. Either use the property or the ivar. It's confusing to use both like you are.

there is missing one line, its sizeToFit or makeKeyAndVisible()
Just look in the example in the ViewControllerProgrammingGuide

Related

NavigationItem LeftBarButtonItem Custome View Auto Layaut

I am trying to set a costume navigationItem.leftBarButtonItem from a nib, but i get an weird behaviour in landscape.
EDIT:
To be clear the view contains a button and a label,this is why i am using a costume view, both button and label have constraints.
self.buttonView =//init;
[self.buttonView setFrame:CGRectMake(0, 0, 36, 36)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.buttonView];
[self.buttonView setBackgroundColor:[UIColor redColor]];
//you will see why
Seems that autolayout is trying to satisfy some constraints thus its expanding your button's frame.
To fix this set the contentHugging priority to required for your button:
[self.buttonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
Try that and let me know if it works
This is the only workaround i could find asap, if someone have a better idea please feel free to share
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self setMenuButtonFrame];
}
-(void)setMenuButtonFrame
{
self.menuView.frame = CGRectMake(self.menuView.frame.origin.x,
self.menuView.frame.origin.y,
self.navigationController.navigationBar.frame.size.height-8,
self.navigationController.navigationBar.frame.size.height-8);
}

setting the frame of a customView in UIBarButtonItem

I am doing
mapButton *topMapButton = [[mapButton alloc] init];
[topMapButton setSize:CGSizeMake(70, 30)];
topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);
UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
[topMapButton addTarget:self action:#selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = barIcon;
where mapButton is a UIButton subclass, with preset look, nothing else
No matter what frame I set for topMapButton, it keeps being in (0,0) and not in the rightBarButtonItem usual place.
Note that using autolayout programatically to set the place works for placing, but crashes when I push another controller in the stack...
Am I doing anything wrong?
Look at this answer
Shortly, you can set edge inset. override alignmentRectInsets for your uibutton subclass and change top and left insets.
- (UIEdgeInsets)alignmentRectInsets {
UIEdgeInsets insets = UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);
return insets;
}
OK found the problem..
It seems that UIBarButtonItem doesn't really care what frame the customView has, since it will set it anyway.
however, my custom button MUST have a custom type, otherwise it will mess the frame and keep it (0,0) of the application screen.
self = [UIButton buttonWithType:UIButtonTypeCustom];

UINavigationBar title text partially appears on screen

I am creating a UINavigationBar programmatically, which is displaying as intended in iOS 7. However, in iOS 8.x all the text in the UINavigationBar doesn't appear within the bounds of the screen. One of my assumptions is this has something to do with Auto Layout.
//ViewControllerRootHomeCenter.m
_navBar = [[UINavigationBar alloc] init];
_navBar.frame = CGRectMake(0,0, self.view.frame.size.width, 64);
// add hambuger menu
//...etc etc
[self.view addSubView:_navbar];
You can try to obtain the screen width this way too:
CGRect frame = (CGRect){0,0,CGRectGetWidth([[UIScreen mainScreen]bounds]),64);
Perhaps it will fix your problem.
First of all, make sure you're not hard-coding the width of the navigation bar.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
Secondly, if your auto layout is enabled, make sure there are no conflicting constraints. If it's not enabled, then I don't see why you should be having this problem.

UIBarButton Item on screen edge in iOS 8 when used in standalone view

In my UINavigation bar added to a XIB with a number of UIViews the positioning of the left and right bar button item is way off:
The view in the XIB simply has an outlet to a view controller, but is not the main view. It's shown via:
[UIView transitionFromView:self.view toView:self.settingsView duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
I have another simpler view which is directly bound to the view property of a view controller which - as expected - looks just normal.
All the views have auto layout. The constraints are fine. I tried a number of different things, but couldn't come up with a fix (or a reason, for that matter). The navigation bar and items are just plain vanilla bar button items without anything like appearance proxies etc...
In the 7.1 sim everything looks just normal.
Anyone seen this before?
Thanks
[EDIT]
I found a solution but not the reason:
If I instead of
[UIView transitionFromView:self.view toView:self.settingsView duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
which adds the new view as a subview of the UIWindow,
use this:
[UIView transitionWithView:self.view duration:0.2 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[self.view addSubview:self.settingsView];
} completion:nil];
which adds the new view as a subview of the existing UIViewControllers view, everything is fine.
I wonder if this a bug and UINavigationBars render in a strange way if contained in a view that is dynamically added to the window...
Not sure whats happening with UIBarButtonItem. It should be arrange by auto layout and it has to be work well. May be one of the constraint conflict with other or misguided.
If you are not able to resolve it. I have one more solution for you. You need to create UIBarButtonItem programatically in your VC.
The idea is to assign space from left and right padding before you add UIBarButtonItem.
Below code will guide you to do the trick.
UIBarButtonItem *leftPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFixedSpace target:self action:nil];
[leftPadding setWidth:5]; // Adjust width for padding from left side.
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAdd target:self action:#selector(addButtonTapped:)];
[self.navigationItem setLeftBarButtonItems:#[leftPadding, addButton]];
UIBarButtonItem *rightPadding = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFixedSpace target:self action:nil];
[rightPadding setWidth:10]; // Adjust width for padding from right side.
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemDone target:self action:#selector(doneButtonTapped:)];
[self.navigationItem setRightBarButtonItems:#[rightPadding, doneButton]];
Add button using the below code, it will definitely work.
CGRect frameimg = CGRectMake(0, 0, 60, 30);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
//[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton setTitle:#"Update" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(updateProfile)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *menuButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem = menuButton;
The issue is in layoutMargins.
Seems like your UINavigationBar has zero margin.
It should have 8 points from each side.

Changing UIToolbar UIBarMetrics

Is there anyway to have the UIBarMetrics of a custom UIToolbar change when the device is rotated? I would prefer it happen automatically but if there is something I can do during rotation that would also be ok. I would like to use UIAppearance so manually adjusting the height of the bar won't work.
I think you need
UIToolbar *toolbar = [UIToolbar appearance];
[toolbar setBackgroundImage:[UIImage imageNamed:#"toolbarLandscapePhone"] forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsLandscapePhone];
You can simple to setBarMetrics to UIBarMetricsLandscapePhone

Resources