move menu button slightly right - ios

I want to have a controller with both back button and menu button as in given image.amazon app
But the problem is the even after increasing the x-axis value it does not shift to right. Hence, I am unable to add back button image. Here's what I am doing right now.
button = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 100, 25)];
[button setImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[button addTarget:[SlideNavigationController sharedInstance] action:#selector(toggleLeftMenu) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[SlideNavigationController sharedInstance].leftBarButtonItem = rightBarButtonItem;

Try this,
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(back)];
UIBarButtonItem *btnMenu = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(menu)];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:btnBack, btnMenu, nil]];

Well on your code you specifically replaced the back button with a barBarButtonItem.
BTW, your naming is a bit confusing you are assigning a rightBarButtonItem to the leftBarButtonItem of the navigationController.
[SlideNavigationController sharedInstance].navigationItem.leftBarButtonItems= #[leftButton,menuButton]; // this will assign both button on the left side. same thing for rightBarButtonItems

Related

Programmatically add a back button to a UINavigationBar?

I am using the following code to achieve a navigation bar in my app. (my app was crashing when I used a push segue so I need a modal segue meaning the nav bar is hidden after the modal segue is called)
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
Does any one know any methods I can use with the above code to achieve back button functionality in the nav bar??
Use below code to add back button on left side of navigation bar.Add UIBarButtonItem on Navigation bar.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
Use below code to add back button on left side
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];
UINavigationItem *item = [[UINavigationItem alloc]
init];
navbar.items= #[item];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnClicked:)];
item.leftBarButtonItem = backButton;
BackButton is better than LeftButton I think:
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] init];
[desVC.navigationItem setBackBarButtonItem:backBtn];
Add Back Button With Image :
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:#"backBtn"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
Regarding the back button, the proper way is not to create your own.
The UINavigationItem class has proper support for this:
func setHidesBackButton(_:animated:)
var leftItemsSupplementBackButton: Bool
So, if you want to add a custom button on the left side, just set leftItemsSupplementBackButton to true.
Or call
self.navigationItem.setHidesBackButton(false, animated:false)

UIToolbar left and right paddings are initially negative

I'm struggling with UIToolbar's strange behaviour in iOS7+ iPhone application.
Here's how I created it:
//toolbar
self.bottomToolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.bounds.size.height-44.0, self.view.bounds.size.width, 44.0)];
self.bottomToolbar.autoresizingMask=(UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth);
[MKUIHelper toolbar:self.bottomToolbar setBarStyle:MKBarStyleWhite];
self.bottomToolbar.items=#[];
[self.view addSubview:self.bottomToolbar];
When I need to update it I'm calling (actually updating only one of its items but this does not matter):
-(void)updateToolbar
{
UIBarButtonItem *doneBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:#"done" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonDidPress:)];
doneBarButtonItem.tintColor=MK_Color_Green;
UIBarButtonItem *clearBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:#"clear" style:UIBarButtonItemStylePlain target:self action:#selector(clearButtonDidPress:)];
clearBarButtonItem.tintColor=MK_Color_Pink;
UIBarButtonItem *spaceBetweenBackAndQuantity=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceBetweenQuantityAndClear=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *quantityItem=[self quantityBarButtonItem];
self.bottomToolbar.items=[NSArray arrayWithObjects:doneBarButtonItem, spaceBetweenBackAndQuantity, quantityItem, spaceBetweenQuantityAndClear, clearBarButtonItem, nil];
[self.rootViewController reloadToolbar];
}
Both UIBarButtonItem objects that was created using initWithTitle:.. have negative left and right paddings somehow. It can be roughly fixed by adding additional fixed space type UIBarButtonItems to both sides of the items array. In other words: compensate strange initial left and right paddings with fixed space type UIBarButtonItems.
Help me someone, please. What causes such strange effect? How can I get rid of this strange padding?
Here's how it looks like:
If you set the width of barButtons to correct width, you may be able to work around this.
- (UIBarButtonItem *)barButtonWithTitle:(NSString *)title action:(SEL)action {
UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:action] autorelease];
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:title forState: UIControlStateNormal];
[button sizeToFit];
CGSize size = button.bounds.size;
[barButtonItem setWidth:size.width];
return barButtonItem;
}

Can't set image to UINavigationBar's rightTabBarItem

I'm trying to set image to rightBarButtonItem.
I tried to do this 2 ways:
/* first way */
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:#selector(rightBarButtonItemTapped) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
/* second way */
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightBarButtonItemTapped)];
P.S. When I tap right side of navigation bar I see that rightBarButtonItemTapped method invoked.
You can set an image to a bar button using the attribute inspector.

Any idea why the custom nav bar back button image isn't displaying with this code?

Any idea why the custom nav bar back button image isn't displaying with this code?
instead of showing the uiimage it's showing the "< Back" default iOS button in text only
[self.navigationController setNavigationBarHidden:NO animated:NO];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"freccia.png"] landscapeImagePhone:nil style:UIBarButtonItemStylePlain target:nil action:#selector(backBtnPress)];
could it be this parameter? UIBarButtonItemStylePlain
thanks
use this code:
UIImage *imageBack = [UIImage imageNamed:#"yourImage.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.bounds = CGRectMake(0, 0, imageBack.size.width, imageBack.size.height);
[btnBack setBackgroundImage:imageBack forState:UIControlStateNormal];
[btnBack addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonBack = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = barButtonBack;
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
You usually set the back button in the parent view controller. However, it might just be easier to use leftBarButtonItem instead of backBarButtonItem.
you can solve this problem by two ways
1. use leftBarButtonItem instead of backBarButtonItem
2. try the below code
self.navigationItem.backBarButtonItem =[[UIBarButtonItem alloc] initWithTitle:#" " style:UIBarButtonItemStylePlain target:nil action:nil];

How to make leftBarButtonItem looking like backBarButtonItem?

The default solvings are not appropriate:
to change the previous ViewController title - I need to make my own function controlling the button touches up
to make a leftBarButtonItem and hide backBarButtonItem - leftBarButtonItem doesn't look like a default backBarButtonItem.
Here's an example to create a custom leftBarButtonItem:
UIImage *buttonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:#selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;
Hope it helps...
Edit:
Try this...
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:#selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;
I don't remember what type of button is the backbutton, try to change the default style to other. Good luck!
Just create a custom barbuttonitem and customize it with a picture similar to the back button item.
You can get its text from the view controllers in the navigation stack.
Then handle the taps to implement the desired effect
You can assign your custom views as the bar buttons to your navigation controller, like this:
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];
self.navigationItem.leftBarButtonItem = customItem;
[customItem release];
Check this Apple documentation for a more detailed explanation, if you want.
Here is a hack to add a back-like button:
UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:#"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];
You can add more items (such like title, right button) this way:
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];

Resources