UIToolbar in UINaviationItem weird line at the top - ios

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

Related

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.

NSInvalidArgumentException', reason: '-[UILabel view]: unrecognized selector sent to instance 0x7ffbd0da2680'

Below is the code where i get this error.
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UILabel *lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,5,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor whiteColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, lbl1, people, food, nature, sports, cats, space]];
Your mistake is in this line because you add label in toolBar. Use this code
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, self.view.bounds.size.height - 44.0f, self.view.bounds.size.width, 44.0f)];
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:toolBar];
UIBarButtonItem *people = [[UIBarButtonItem alloc] initWithTitle:#"people" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *food = [[UIBarButtonItem alloc] initWithTitle:#"food" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *nature = [[UIBarButtonItem alloc] initWithTitle:#"nature" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *sports = [[UIBarButtonItem alloc] initWithTitle:#"sports" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *cats = [[UIBarButtonItem alloc] initWithTitle:#"cats" style:UIBarButtonItemStylePlain target:self action:#selector(tap:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
lbl1 = [[UILabel alloc] init];
[lbl1 setFrame:CGRectMake(0,64,100,20)];
lbl1.backgroundColor=[UIColor clearColor];
lbl1.textColor=[UIColor redColor];
lbl1.userInteractionEnabled=YES;
[self.view addSubview:lbl1];
lbl1.text= #"TEST";
[toolBar setItems:#[space, people, food, nature, sports, cats, space]];
toolBar item clicked event
-(IBAction)tap:(UIBarButtonItem*)sender{
lbl1.text= sender.title;
}

Setting the appearance of just one UIToolbar in iOS 8

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.

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

UIToolbar not showing

I want a UIToolbar to show up when I click on a certain UITextField, but the toolbar, defined in the #interface, doesn't show up.
This is my code for initializing and attaching it to the text field:
toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(isDone:)], nil];
toolbar.hidden = NO;
[toolbar sizeToFit];
angleField.inputAccessoryView = toolbar;
Can someone see what I'm doing wrong?
P.S. I had used similar-looking code in another project, and it worked.
Here is the code:
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneWithNumberPad:)],
nil];
[numberToolbar sizeToFit];
changes.inputAccessoryView = numberToolbar;
where changes is a UITextField
I'm not sure if this is it, but what I did was instead of calling setItems: on the toolbar, I called setItems:animated: and now it works...
Thank you to all who responded and took the time to try to help me.
Happy coding!

Resources