How to add more zhan two UIBarButtonItem to rightBarButtonItem? - ios

I want add three buttons to rightBarButtonItem , but it just display two.
I also try to add a UIView(buttons in the view) to self.view ,but its below the navigation.

Something Like this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addButton:)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:#selector(showAllButton:)];
UIBarButtonItem *searchButton2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
target:self
action:#selector(showAllButton:)];
self.navigationItem.rightBarButtonItems = #[searchButton, addButton, searchButton2];

YourViewontroller.h
UIView *view_navigation;
viewcontroller.m
- (void)viewDidLoad
{
view_navigation = [[UIView alloc]initWithFrame:CGRectMake(200, 5, 100, 40)];
view_navigation.backgroundColor = [UIColor clearColor];
UIButton *add_button = [UIButton buttonWithType:UIButtonTypeCustom];
[add_button setBackgroundImage:[UIImage imageNamed:#"Add-icon.png"] forState:UIControlStateNormal];
[add_button addTarget:self action:#selector(Add_Student:) forControlEvents:UIControlEventTouchUpInside];
[add_button.imageView setContentMode:UIViewContentModeScaleToFill];
[add_button setFrame:CGRectMake(0, 8, 25, 25)];
[view_navigation addSubview:add_button];
UIButton *Remove_button = [UIButton buttonWithType:UIButtonTypeCustom];
[Remove_button setBackgroundImage:[UIImage imageNamed:#"Delete_icon.jpeg"] forState:UIControlStateNormal];
[Remove_button addTarget:self action:#selector(Remove_Student:) forControlEvents:UIControlEventTouchUpInside];
[Remove_button.imageView setContentMode:UIViewContentModeScaleToFill];
[Remove_button setFrame:CGRectMake(40, 8, 25, 25)];
[view_navigation addSubview:Remove_button];
UIButton *Edit_button = [UIButton buttonWithType:UIButtonTypeCustom];
[Edit_button setBackgroundImage:[UIImage imageNamed:#"Edit_icon.jpeg"] forState:UIControlStateNormal];
[Edit_button addTarget:self action:#selector(Edit_Student:) forControlEvents:UIControlEventTouchUpInside];
[Edit_button.imageView setContentMode:UIViewContentModeScaleToFill];
[Edit_button setFrame:CGRectMake(80, 8, 25, 25)];
[view_navigation addSubview:Edit_button];
[self.navigationController.navigationBar addSubview:view_navigation];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewWillDisappear:(BOOL)animated
{
[view_navigation removeFromSuperview];
[super viewWillDisappear:animated];
}

Related

How to change the position of buttons in uitoolbar in ios and set a badge for button to show the counts

I am having a toolbar and in that i have 3 buttons i gave the code as below
UIButton *likeImage = [UIButton buttonWithType:UIButtonTypeCustom];
[likeImage setFrame:CGRectMake(0, 0, 30, 30)];
[likeImage setBackgroundImage:[UIImage imageNamed:#"likeIconDefault"] forState:UIControlStateNormal];
[likeImage addTarget:self action:#selector(likeImagePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *likeImageButton = [[UIBarButtonItem alloc] initWithCustomView:likeImage];
UIButton *commentImage = [UIButton buttonWithType:UIButtonTypeCustom];
[commentImage setFrame:CGRectMake(200, 0, 30, 30)];
[commentImage setBackgroundImage:[UIImage imageNamed:#"commentIcon"] forState:UIControlStateNormal];
[commentImage addTarget:self action:#selector(commentImagePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *commentImageButton = [[UIBarButtonItem alloc] initWithCustomView:commentImage];
UIButton *dislikeImage = [UIButton buttonWithType:UIButtonTypeCustom];
NSArray *buttonItems = [NSArray arrayWithObjects: likeImageButton, commentImageButton, nil];
position of the button is not changing even after giving different values for X and Y coordinates

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

UIBarButtonItem with image doesn't show

I'm trying to set a UIBarButtonItem with an image. The problem is that it never appears. When I just add a UIBarButtonItem with a title it works.
UIBarButtonItem *openLeftSliderButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"filter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openLeftSlider)];
self.navigationItem.leftBarButtonItem = openLeftSliderButton;
UIImage *sideImage = [UIImage imageNamed:#"i1.jpg"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, 20, 20)];
[btnSideMenu addTarget:self action:#selector(btnLeftMenuClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
self.navigationItem.leftBarButtonItem=sideMenuItem;
You can create UIBarButtonItem using initWithCustomView
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[_leftButton addTarget:self action:#selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton sizeToFit];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];

Space between uibarbuttons in navigation bar, Xcode 6.1

I have tried almost all answers here, How to remove the blank space between the two uibarbuttons shown below the code i use is
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
UIBarButtonItem *backkb = [[UIBarButtonItem alloc] initWithCustomView:backBTN];
// self.navigationItem.leftBarButtonItem = backkb;
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
UIBarButtonItem *btnhome = [[UIBarButtonItem alloc] initWithCustomView:homeBTN];
// btnhome.imageInsets = UIEdgeInsetsMake(-10, 0, 0, 0);
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: backkb, btnhome, nil]];
self.navigationItem.hidesBackButton = YES;
Try this,
// Create a UIView to add both buttons
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
UIButton *backBTN= [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[backBTN addTarget:self action:#selector(backACT:) forControlEvents:UIControlEventTouchUpInside];
[backBTN setBackgroundImage:[UIImage imageNamed:#"menu_icon.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:backBTN];
UIButton *homeBTN= [[UIButton alloc] initWithFrame:CGRectMake(30, 0, 30, 30)];
[homeBTN addTarget:self action:#selector(homeACT:) forControlEvents:UIControlEventTouchUpInside];
[homeBTN setBackgroundImage:[UIImage imageNamed:#"logout_btn.png"] forState:UIControlStateNormal];
//Add button to UIView
[leftView addSubview:homeBTN];
//Set UIView as CustomView for bar button
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftView];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects: leftBarButton, nil]];
self.navigationItem.hidesBackButton = YES;
I have tried this and got output as
used some background colour to know the borders

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.

Resources