Question

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:

enter image description here

Any ideas what's causing this? Thanks!

Was it helpful?

Solution

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]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top