UINaviationItem中的UIToolbar位于顶部的奇怪行

时间:2014-05-03 08:51:42

标签: ios iphone objective-c cocoa-touch

我试图通过以下方式在UINavigationItem的titleView中显示UIToolbar:

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;

但是我在工具栏的顶部得到了这条奇怪的黑线:

enter image description here

任何想法导致了什么?谢谢!

1 个答案:

答案 0 :(得分:2)

我不知道发生这种情况的方式,但我和你的一样有同样的问题我将其修复为使用

toolbar.clipsToBounds = YES 

您也可以尝试以下
OR

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
                                  forBarPosition:UIBarPositionAny
                                      barMetrics:UIBarMetricsDefault];

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];