以编程方式添加UINavigationBar和状态栏

时间:2017-09-28 22:32:30

标签: ios storyboard

我正在以编程方式创建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

3 个答案:

答案 0 :(得分:0)

你可以创建一个UINavigationViewController并在UINavigationViewController中推送你的UIViewController,如下所示:

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

答案 1 :(得分:0)

一个可能的解决方案是嵌入UINavigationController,因为azamsharp指出。

另一种方法如下。

使你的ViewController成为UINavigationBarDelegate,在创建UINavigationBar时将self设置为委托,然后实现 - (UIBarPosition)positionForBar:

样品:

- (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;
}

答案 2 :(得分:0)

在Swift 4.1中尝试过此解决方案

let menuBar: UIView = {
    let mb = UIView()
    mb.backgroundColor = .red
    mb.translatesAutoresizingMaskIntoConstraints = false
    return mb
}()

private func setupMenuBar(){
    view.addSubview(menuBar)

    let constraints = [ menuBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
                        menuBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
                        menuBar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
                        menuBar.heightAnchor.constraint(equalToConstant: 50)]
    NSLayoutConstraint.activate(constraints)
}