2017-09-28 49 views
0

我以編程方式創建UINavigationBar,並且希望它在狀態欄下擴展。這裏是代碼:以編程方式添加UINavigationBar和狀態欄

_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];[_baseView setBackgroundColor:[UIColor redColor]]; 
self.view = _baseView; 

_navigationBar = [[UINavigationBar alloc] init]; 

[_navigationBar setBarTintColor:[UIColor greenColor]]; 
[_navigationBar setBackgroundColor:[UIColor purpleColor]]; 

UINavigationItem* navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title!"]; 
[_navigationBar setTitleTextAttributes:self.navigationController.navigationBar.titleTextAttributes]; 
[_navigationBar pushNavigationItem:navigationItem animated:NO]; 

[_navigationBar setTranslatesAutoresizingMaskIntoConstraints:NO]; 

[self.view addSubview:_navigationBar]; 

[_navigationBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES; 
[_navigationBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES; 
[_navigationBar.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES; 
[_navigationBar.heightAnchor constraintEqualToConstant:64].active = YES; 

不幸的是,輸出不太正確。正如你在下面的圖片中看到的那樣,整個UINavigationBar是正確的尺寸,紫色,但標題和其餘部分的大小不一樣。

我在這裏錯過了什麼?

enter image description here

回答

0

一個可能的解決方案是在一個UINavigationController嵌入作爲azamsharp指出。

另一種方式如下。

樣品:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; 
    [_baseView setBackgroundColor:[UIColor redColor]]; 
    self.view = _baseView; 

    _navigationBar = [[UINavigationBar alloc] init]; 

    [_navigationBar setBarTintColor:[UIColor greenColor]]; 
    [_navigationBar setBackgroundColor:[UIColor purpleColor]]; 

    [_navigationBar setDelegate:self]; 

    UINavigationItem* navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title!"]; 
    [_navigationBar setTitleTextAttributes:self.navigationController.navigationBar.titleTextAttributes]; 
    [_navigationBar pushNavigationItem:navigationItem animated:NO]; 

    [_navigationBar setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [self.view addSubview:_navigationBar]; 

    [_navigationBar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = YES; 
    [_navigationBar.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = YES; 
    if (@available(iOS 11.0, *)) { 
     [_navigationBar.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor].active = YES; 
    } else { 
     [_navigationBar.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor].active = YES; 
    } 
} 

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 
    return UIBarPositionTopAttached; 
} 
0

您可以創建一個UINavigationViewController,推動UIViewController中的UINavigationViewController內,如下圖所示:

let dishesTableViewController = DishesTableViewController() 
let navigationController = UINavigationController(rootViewController: dishesTableViewController) 
+0

我寧願沒有,如果我用這個方法(UIBarPosition)positionForBar -

讓您的ViewController一個UINavigationBarDelegate,創建UINavigationBar的,然後實現當設置自拍不如授能夠;儘管這完全有效。 –

+0

爲什麼不使用這種方法? – azamsharp

相關問題