【问题标题】:iOS: Error when trying to customize Navigation bariOS:尝试自定义导航栏时出错
【发布时间】:2012-11-13 21:26:40
【问题描述】:

我正在尝试自定义导航栏的标题视图并收到错误消息。以下是相关代码:

UIView *topView = [[UIView alloc] init];
    topView.translatesAutoresizingMaskIntoConstraints=NO;

     self.navigationItem.titleView=topView;

    UIImage *image = [UIImage imageNamed: @"logojim_35.png"];

    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    imageView.translatesAutoresizingMaskIntoConstraints=NO;
    [topView addSubview:imageView];

    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.translatesAutoresizingMaskIntoConstraints=NO;
    titleLabel.text=@"ACPL Mobile";

    [topView addSubview:titleLabel];

    NSDictionary *views = NSDictionaryOfVariableBindings(topView,imageView,titleLabel);

    [topView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageView]-[titleLabel]|" options:nil metrics:nil views:views]];

这是我得到的错误:

2012-11-13 16:23:51.635 Mobile_ACPL[22177:12b03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UINavigationBar's implementation of -layoutSubviews needs to call super.'

知道问题出在哪里吗?

是的,我的项目需要自动布局。

【问题讨论】:

  • 你覆盖layoutSubviews吗?如果你这样做,你会打电话给super吗?
  • 这段代码是以什么方式运行的?
  • @JoePasq 这是在视图控制器的 viewDidLoad 中调用的。
  • @AlexWayne 我没有继承 UINavigationBar。我需要吗?我试图创建一个视图并将其分配给导航栏的“titleView”属性。只要我不尝试添加约束,这似乎就可以工作。

标签: iphone ios uinavigationbar uinavigationitem autolayout


【解决方案1】:

看看这个问题:Building a titleView programmatically with constraints (or generally constructing a view with constraints)

如果你不设置一个框架,标题视图不知道 x 和 y 与它的父级相关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-04
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多