我希望我的应用程序顶部有一个UINavigationBar,就像在手机应用程序中一样,但我不想将它与UINavigationController一起使用,也就是说,我想将它作为一个“独立”对象使用。
将拥有UINavigationBar的视图控制器将其视图保存在.xib文件中。我试图从对象库中拖动一个UINavigationBar实例,它运行良好,但是应用程序的状态栏仍然是白色的,而UINavigationBar是灰色的。我希望状态栏具有相同的灰色色调,而不是视图的其余部分。
为了告诉你我的意思,我有两张照片。第一个是手机应用程序。注意状态栏和导航栏是灰色的,但是背景是白色的。

下面的图片来自我的应用程序,因为你可以看到状态栏是白色的(我希望它也是灰色的)。

我还尝试在视图控制器中使用以下代码。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Köp ProViva";
}我尝试过使用和不使用UINavigationBar,但是要么没有显示导航条,要么它看起来和以前一样。
如何添加一个UINavigationBar并使状态栏具有相同的颜色?
发布于 2014-04-16 11:00:57
您可以实现UINavigationBarDelegate协议的委托方法UINavigationBarDelegate,只需返回UIBarPositionTopAttached。
示例:
-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
return UIBarPositionTopAttached;
}//if you're not using a `UINavigationController` and instead
//simply want a `UINavigationBar` then use the following method as well
-(void)testMethod
{
UINavigationBar *navBar = [[UINavigationBar alloc] init];
[navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
[navBar setBarTintColor:[UIColor lightGrayColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
}希望这能有所帮助。
发布于 2014-04-16 10:50:01
//创建普通导航条
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.topItem.title = @"title";
[self.view addSubview:headerView];发布于 2014-04-16 11:18:42
您不能直接修改状态栏背景色,但由于它是透明的(自iOS7),您可以在状态栏后面放置一个20 if的UIView,并将其显示为具有背景色的状态栏。
至于UINavigationBar,它只是修改了一个UINavigationBar对象,这将有所帮助。
示例:
- (void)viewDidLoad
{
[super viewDidLoad];
//...
//ISSUE 1: StatusBar Background
UIView *vwStatusBarUnderlay = [[UIView alloc] init];
[vwStatusBarUnderlay setFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
[vwStatusBarUnderlay setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:vwStatusBarUnderlay];
//[vwStatusBarUnderlay sendSubviewToBack:self.view];
//ISSUE 2: NavigationBar
UINavigationBar *navBar = [[UINavigationBar alloc] init];
[navBar setFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
[navBar setBarTintColor:[UIColor lightGrayColor]];
[navBar setTranslucent:NO];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
[navItem setTitle:@"Favoriter"];
[navItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]];
[navBar setItems:@[navItem]];
[self.view addSubview:navBar];
}https://stackoverflow.com/questions/23107231
复制相似问题