Setting the appearance of just one UIToolbar in iOS 8 - ios

I have two toolbars stacked on top of each other. They are created programmatically. The bottom bar uses standard UIButton elements with a solid colour background and the buttons inherit the tint.
I want the toolbar above it however to have a background image. I can only get this to work by using the appearance setter, however the appearance property is applied to both toolbars. Can anyone see a way around this?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
///top toolbar///
UIToolbar *toolbar2 = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-84, self.view.bounds.size.width, 44)];
toolbar2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolbar2];
UIImage *testimage = [[UIImage imageNamed:#"test.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:testimage forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];
///////bottom toolbar////
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-44, self.view.bounds.size.width, 44)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
toolbar.barTintColor=[UIColor colorWithHexValue:0xff336600];
[self.view addSubview:toolbar];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"help.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openHelpView)];
button1.tintColor=[UIColor whiteColor];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *button2=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"web.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openWebView)];
button2.tintColor=[UIColor whiteColor];
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"fb-icon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openFBView)];
button3.tintColor=[UIColor whiteColor];
UIBarButtonItem *button4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"twitter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openTwitterView)];
button4.tintColor=[UIColor whiteColor];
[toolbar setItems:[[NSArray alloc] initWithObjects:button1,spacer,button3,spacer,button4,spacer,button2, nil]];
[self.view addSubview:toolbar];

Figured it out.
Just created an empty subclass of the UIToolbar class and then created my top Toolbar from that instead. That way I can apply the appearance property separately.

Related

How to remove NavigationBar Right space?

I'm playing with Navigation Bar Buttons I need a button from right edge with 0 space, after google search i found below code
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -25;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
But unfortunatly in iOS 13 negativeSpacer not working. can anybody suggest me solutions.
Attached screenshot here
Thanks.
Let's try this easy way, Let's define the width of space BarButtonItem to be width of self.view.frame.size.width - width of the button.
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc]
initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil
action:nil];
negativeSpacer.width = self.view.frame.size.width - 45; // As 45 is the frame width of your filter button
[self.navigationItem setRightBarButtonItems:[NSArray
arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
Hope this helps!

Right bar button does not appear

I try to add UINavigationBar programmatically and set bar button items.
I tried:
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.leftBarButtonItem = bbiDone;
navItem.rightBarButtonItem = bbiTry;
self.artificialNavBar.items = #[ navItem ];
[self.view addSubview:self.artificialNavBar];
However, only left bar button appears, right one is hidden. Did i miss something?
Declare
#property (nonatomic, strong) UINavigationItem *navItem;
in the .h file of this class where you have written all these and then replace your code with below code.
self.artificialNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
self.artificialNavBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.artificialNavBar];
UIBarButtonItem *bbiDone = [[UIBarButtonItem alloc] initWithTitle:#"Готово" style:UIBarButtonItemStyleDone target:nil action:nil];
UIBarButtonItem *bbiTry = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navItem = [[UINavigationItem alloc] init];
[self.navItem setLeftBarButtonItem:bbiDone animated:NO]
[self.navItem setRightBarButtonItem:bbiTry animated:NO]
[self.artificialNavBar setItems:#[self.navItem] animated:NO]
and make sure the navbar is nonatomic and strong.

How to make a UIBarButtonItem selectable on a UIToolbar part of a UIPickerView

I have a two column UIPickerView in a project that I am working on, and I successfully created the UIPickerView, UIToolBar, and UIToolBarItem programmatically. However the toolbar item isn't responding to touch inputs. I have tried on the device and in the Simulator, and the selector method is never being called / reached.
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 216)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnAddCredit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addCreditToUser:)];
NSLog(#"btnAddCredit %hhd",btnAddCredit.isEnabled);
[toolBar setItems:[NSArray arrayWithObjects:flex,btnAddCredit,nil]];
toolBar.userInteractionEnabled = true;
[_pickerView addSubview:toolBar];
_pickerView.delegate = self;
_pickerView.showsSelectionIndicator = YES;
[self.parentViewController.view addSubview:_pickerView];
Here's a quick animated GIF explaining what is happening,

UIToolbar in UINaviationItem weird line at the top

I am trying to display a UIToolbar in UINavigationItem's titleView via:
UIBarButtonItem *mapItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"world_icon"]
style:UIBarButtonItemStylePlain
target:self action:#selector(hi)];
UIBarButtonItem *mapItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"world_icon"]
style:UIBarButtonItemStylePlain
target:self action:#selector(hi)];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[toolbar setItems:#[mapItem, spaceItem, mapItem2, spaceItem]];
toolbar.backgroundColor = [UIColor clearColor];
toolbar.barStyle = UIBarStyleDefault;
[toolbar setBackgroundImage:[UIImage new]
forToolbarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
toolbar.tintColor = [UIColor whiteColor];
self.navigationItem.titleView = toolbar;
But I am getting this weird black line at the top of the toolbar:
Any ideas what's causing this? Thanks!
I don't know way this happening, but I had same issue like a yours and I fixed it as use
toolbar.clipsToBounds = YES
Also you can try with following
OR
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

how to add two button in navigation bar with some space

I am adding two button in navigation bar they are working fine but i want space between them they are both combined i want a bit space between them
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIBarButtonItem *btnEdit = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIToolbar *rightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
rightToolBar.backgroundColor = [UIColor clearColor];
rightToolBar.tintColor = [UIColor colorWithRed:40.0/255.0 green:48.0/255.0 blue:51.0/255.0 alpha:0.0];
NSArray *buttonsRight = [NSArray arrayWithObjects:btnEdit, btnAdd, nil];
[rightToolBar setItems:buttonsRight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightToolBar];
You can add any of these two between your UIBarButtonItem
UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
Note that to set the width of a Fixed Space UIBarButtonItem, you need to set the .width property
[fixed setWidth:455.0f];

Resources