Space between uibarbuttons in navigation bar, Xcode 6.1 - ios

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

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

UINavigationBarItem custom - how to get the corner of tabcontroller?

i want to ask how i can get the icon of navigationtabbaritem to the left corner (left:0) and right corner (right:0).
my code :
_custombtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_custombtn.backgroundColor = [UIColor redColor];
[_custombtn setBackgroundImage:[UIImage imageNamed:#"ic_msg.png"]forState:UIControlStateNormal];
[_custombtn addTarget:self action:#selector(viewBn:) forControlEvents:UIControlEventTouchUpInside];
self.csbtn = [[UIBarButtonItem alloc]initWithCustomView:_custombtn];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(0.0, 0.0, 44, 44);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(rightBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *spaceFix = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
spaceFix.width = -10;
self.navigationItem.rightBarButtonItems = #[spaceFix, aBarButtonItem];
Please add spaceFix for resolve your problem may this help lot

How to add more zhan two UIBarButtonItem to rightBarButtonItem?

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

Label keeps disappearing from my navigation menu

For some reason the label in my navigation bar keeps disappearing and only displaying my map icon and home button. Is it because I'm adding it as a barbuttonitem? Is there a way around this?
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(160, 5, 250, 70)];
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0,5, 310, 70)];
l1.text=#"FIND THINGS TO DO";
l1.font=[UIFont fontWithName:#"Helvetica Neue LT Std" size:25.0];
l1.backgroundColor=[UIColor clearColor];
l1.textColor=[UIColor whiteColor];
l1.textAlignment=UITextAlignmentCenter;
[view1 addSubview:l1];
UIBarButtonItem *left2=[[UIBarButtonItem alloc]initWithCustomView:view1];
self.navigationItem.rightBarButtonItem=left2;
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBarHidden = NO;
UIImage* image3 = [UIImage imageNamed:#"home_btn.png"];
homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 38, 38)];
[homeBtn setBackgroundImage:image3 forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(Home)
forControlEvents:UIControlEventTouchUpInside];
[homeBtn setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
NSArray *arr=[[NSArray alloc]initWithObjects:home, nil];
self.navigationItem.rightBarButtonItems = arr;
UIImage* mapButton1 = [UIImage imageNamed:#"Top-Map-Icon.png"];
mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 30)];
[mapButton setBackgroundImage:mapButton1 forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(Map)
forControlEvents:UIControlEventTouchUpInside];
[mapButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *map = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
NSArray *arr2 = [[NSArray alloc] initWithObjects:map, nil];
self.navigationItem.leftBarButtonItems = arr2;

Resources