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];
Related
I have created a ViewController Using Xib file of size wAny and hAny and i have used autolayaout and constraint to fit UI all devices and all orientation.
Everything is working fine but when i'm running app on iPhone portrait mode title of navigation bar is not centred where as when rotating it to landscape mode title is centred.
I have used following code for setting title
self.title = #"Download Schedule";
Already tried:
I have tried to set custom UILabel of lesser font size to Navigation bar's title view but still i'm getting it on left side .
Here is code of my right button
//add right bar button for more options
UIView *navBarButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIBarButtonItem *item0 = [UIBarButtonItem negativeSpacerWithWidth:5];
UIButton *moreButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[moreButton setBackgroundImage:[UIImage imageNamed:#"ic_web"] forState:UIControlStateNormal];
[moreButton addTarget:self action:#selector(addNewSchedule) forControlEvents:UIControlEventTouchDown];
[navBarButtonsView addSubview:moreButton];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:navBarButtonsView];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item0, backButton, nil];
There is something else going on - something you have not told us about. I suspect that there is something wrong with how you are setting your rightButtonItems, such that there are invisible buttons taking up the space on the right. I can reproduce this issue only by deliberately making the right button really wide:
So I suspect you have applied a large width to the right button, or there are extra invisible right button items taking up the space on the right.
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);
}
I have an issue with my navigation bar, the UIBarButtonItem is not correctly vertically aligned. I don't know why because I'm using a simple UIBarButtonItem and not a custom view. See my code below and thanks for your help!
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithTitle:#"New Game" style:UIBarButtonItemStyleDone target:self action:#selector(newGame)];
self.navigationItem.rightBarButtonItem = newGameItem;
Actually there are couple ways to align the bar buttons.
Try to adjust the button title position with this:
[self.navigationItem.rightBarButtonItem setTitlePositionAdjustment:UIOffsetMake(-10, -10)
forBarMetrics:UIBarMetricsDefault];
-10 -10 - just for example
OR
you can initialize your newGameItem with custom UIButton. The custom button is a subclass of UIButton with this method overridden
- (UIEdgeInsets)alignmentRectInsets {
return UIEdgeInsetsMake(0, -10, 0, 15.0f); // here you can play with values to align your button properly
}
then initialize your newGameItem
CustomBarButton *button = [CustomBarButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"New game" forState:UIControlStateNormal];
button.frame = CGRectMake (0, 0, 60, 40);
[button addTarget:self action:#selector(newGame) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *newGameItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = newGameItem;
I had a similar issue: We use custom fonts via UIAppearance for both navigation titles and bar button items, and in some cases, the right item was vertically misaligned.
I could easily fix the misalignment by using [self.navigationItem setRightBarButtonItem:rightItem animated:YES] instead of the property setter.
I had the same problem with a button on the left, I was able to fix it in Xcode 6.3.1: select the button (bar button item) right column tab "ruler" inset image Top value: -54 instead of 0.
Note that: my device is an iPhone4 with iOS 7, the button was perfectly in place in Xcode and misplaced on the device, now it is in place on the device and misplaced in Xcode
Unfortunately, there is not much you can do about UIBarButtonItem vertical spacing. I believe you'll have to either increase the font size of your title on the UINavigationBar (you can do this with UIAppearance also), or decrease the font size of your UIBarButtonItem.
I found through another question on here how to change the height of UINavigationBar using a category:
#import "UINavigationBar+Taller.h"
#implementation UINavigationBar (Taller)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width,100); //exaggerated height
return newSize;
}
#end
That works brilliantly.
Is there a way to move the title and navigation buttons up? They are aligning to the bottom of the new size. I want to put something into the bottom of the navigation bar.
Private methods are ok since this isn't going to be an app store candidate.
To change the vertical offset for the title:
CGFloat yourOffset = 5.0; //whatever value you need
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:yourOffset forBarMetrics:UIBarMetricsDefault];
To make the button a bit higher or lower
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Another (more flexible) better approach would be to create a subclass of UINavigationBar
and override layoutSubviews, placing your views in the correct place.
A cusomized UINavigationBar requires me to present a customized "back" button, I use navigationItem.leftBarButtonItem = myCustomizedButton, but its position is fixed.
Would anyone be so kind to share how can I shift this button 40pixels to right?
You can create a containing view that is 40 pixels bigger than your image.
Add your image at 40 pixels offset.
Add the containing view as the leftBarButtonItem.
Code follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];
// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[containingView addSubview:barUIButton];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;
You can add a blank space to your picture that you display on the navBar. I've had the same problem, and it's the only one solution i've found for resolve it. A little bit tricky but it works...