Create custom UIBarButtomItem in storyboard - ios

I need to make custom "Settings" button in navigation bar.
But, unfortunately, I was unable to make in in storyboard editor correctly.
I need to see only my picture, no button rectangle.
Is there are a way to make with kind of button correctly?
Thanks.

You can add this code in the ViewController -viewDidLoad method:
for the left bar button:
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[leftButton setImage:buttonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[leftButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
or for the right bar button:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[rightButton setImage:buttonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[rightButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
There's no need to add a BarButtonItem to your view in the xib or storyboard.
Here's what it could look like:

Related

Custom UIBarButtonItem keep Aspect Ratio for Image but Space out Buttons

I am adding some custom image UIBarButtonItems to my navigation bar. If I do not mess with the item.width property, then my buttons are too close together, but if I space them out with the width property, then the button image is distorted. How can I maintain the aspect ratio of the button image but space out my custom items?
// Create custom map button
UIButton *mapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[mapButton setImage:[UIImage imageNamed:#"map.png"] forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(dismissMapView:) forControlEvents:UIControlEventTouchUpInside];
[mapButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
button1.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Create custom list button
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
[listButton setImage:[UIImage imageNamed:#"list.png"] forState:UIControlStateNormal];
[listButton addTarget:self action:#selector(scrollToRow:) forControlEvents:UIControlEventTouchUpInside];
[listButton setFrame:CGRectMake(280, 25, 30, 30)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:listButton];
button2.width = (self.drawer.frame.size.width / 5.0f); // Space it out a bit
// Add buttons to drawer
self.drawer.items = #[button1,button2];
Try this code for adding custom bar button in navigationbar.
UIImage* image3 = [UIImage imageNamed:#"compose_message_button.png"];
CGRect frameimg = CGRectMake(250, 9, 43,32);
UIButton *SettingButton = [[UIButton alloc] initWithFrame:frameimg];
[SettingButton setBackgroundImage:image3 forState:UIControlStateNormal];
[SettingButton addTarget:self action:#selector(BtnWriteMessage:)
forControlEvents:UIControlEventTouchUpInside];
[SettingButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:SettingButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[SettingButton release];
i hope this code is useful for you.
You can create some explicit spacer items using UIBarButtonSystemItemFixedSpace.
Or you could create your bar buttons with custom views (initWithCustomView:) and then you can lay the content views out as you like.

Navigation Bar custom button

backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];
The problem i am facing is that though the button dimensions are44*44, wherever i tap anywhere around it, the the button action is fired.
Please try the bellow code : its working properly
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Its not a bug. It is a default behaviour. In iPhone, for navigation bar buttons the touch detection is little more expanded rather than its frame. Just have a look on any other application. Everywhere the button get fired if we tap nearer but outside its frame.
It's the intended behaviour, if you really want to limit the area of the touch, you can wrap the button inside a UIView:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];

Customize navigation bar button in iOS6

Update - One Possible Solution
Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
[btn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];
Original Post
In iOS 6, the goal is like this:
while current result is like this:
My code is
btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"button.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickMenuButton)];
button.png is this the white circle with white three bars inside and a tranparent background.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:#"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:#"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:#selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;
I am accomplishing it by
UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_back_ontouch_scr11.png"]];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
[leftButton setImage:uView.image forState:UIControlStateHighlighted];
[leftButton setImage:[UIImage imageNamed:#"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
[leftButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.navigationItem.leftBarButtonItem = aButton;
self.navigationItem.hidesBackButton = YES;
This should work.

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar
Not written again in every interface.
Is there a unified method to uniform setting?
UIImage image = [UIImage imageNamed:imagePath];
UIButton button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
A lot of interface backBarButtonItem need custom,code is the same,I don't want to write N times
You can add this in a singleton global-variables file, like
GlobalVariable.h
+(UIBarButtonItem*)customButton;
GlobalVariable.m
+(UIBarButtonItem*)customButton{
UIImage *image = [UIImage imageNamed:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithCustomView:button];
return btn;
}
Then, import GlobalVariable.h in the views you want the button to appear and call
self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];
You can keep GlobalVariable there, in case you need other variables too.

How to align the title on backbuttonitem

i'm new, so i cann't upload the image, but i have a problem when i customize the backButtonItem in iOS
the code i write:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"backBtn.png"];
backBtn.frame = CGRectMake(0, 0, backBtnImage.size.width, backBtnImage.size.height);
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[backBtn addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[backBtn setTitle:#"Back" forState:UIControlStateNormal];
UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[backBtnItem setStyle:UIBarButtonItemStyleDone];
self.navigationItem.leftBarButtonItem = backBtnItem;
but the title with "back" is not aligned on the backButton.
How could i align the title of a backbuttonitem which has been customized?
Try with this:
backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

Resources