私は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全体が正しいサイズ(紫色)ですが、タイトルとそれ以外の部分は同じサイズではありません。
私はここで何が欠けていますか?
を私は場合は、このアプローチを使用しないことを好むだろうできる;これは完全に有効です。 –
なぜこのアプローチを使用しないのですか? – azamsharp