我以編程方式創建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是正確的尺寸,紫色,但標題和其餘部分的大小不一樣。
我在這裏錯過了什麼?
我寧願沒有,如果我用這個方法(UIBarPosition)positionForBar -
讓您的ViewController一個UINavigationBarDelegate,創建UINavigationBar的,然後實現當設置自拍不如授能夠;儘管這完全有效。 –
爲什麼不使用這種方法? – azamsharp