2

我有一个带有一个 UINavigationBar 和一个 UIView 的视图控制器。我曾尝试使用自动布局,但一旦我将项目添加到 UINavigationBar 就会崩溃:S

@property (nonatomic) UIBarButtonItem  *barbuttonitem;
@property (nonatomic) UINavigationItem *navigationitem;
@property (nonatomic) UINavigationBar  *navigationbar;
@property (nonatomic) UIView           *tempview;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

_barbuttonitem  = [[UIBarButtonItem alloc] initWithTitle:@"menu" style:UIBarButtonItemStylePlain target:self action:@selector(menuButtonPressed)];
_navigationitem = [[UINavigationItem alloc] initWithTitle:@"menu name"];
_navigationitem.rightBarButtonItem = _barbuttonitem;
_navigationbar  = [[UINavigationBar alloc] initWithFrame:CGRectNull];
_navigationbar.items = [NSArray arrayWithObject:_navigationitem]; // <======== If I exclude this line then no crash

_tempview = [[UIView alloc] initWithFrame:CGRectNull];

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_navigationbar, _tempview);

// We already have a list of UIViews so lets use that for trivial stuff
for (id view in [viewsDictionary allValues]) {
    [self.view addSubview:view];
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
}

// Align to left/right borders
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_navigationbar]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDictionary]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tempview]|"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDictionary]];

// Align first to top border and final to bottom border and equal the heights
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_navigationbar(==44)][_tempview]"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:viewsDictionary]];
}

堆栈转储:

 2013-09-10 12:19:42.813 MyApp[1107:a0b] *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]'
 *** First throw call stack:
 (
0   CoreFoundation                      0x0174f6f4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x014cf8b6 objc_exception_throw + 44
2   CoreFoundation                      0x0174f4cb +[NSException raise:format:] + 139
3   QuartzCore                          0x03acc0ea _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 190
4   QuartzCore                          0x03acc2a9 -[CALayer setPosition:] + 68
5   QuartzCore                          0x03acc9af -[CALayer setFrame:] + 799
6   UIKit                               0x0029e33c -[UIView(Geometry) setFrame:] + 302
7   UIKit                               0x003f199e -[UILabel setFrame:] + 184
8   UIKit                               0x0030a0ca -[UINavigationItemView initWithNavigationItem:] + 462
9   UIKit                               0x002e04ea -[UINavigationItem _titleView] + 98
10  UIKit                               0x002fd1ab -[UINavigationBar _removeItemsFromSuperview:] + 133
11  UIKit                               0x002e8c7b -[UINavigationBar _setItems:transition:] + 795
12  UIKit                               0x002e8829 -[UINavigationBar setItems:animated:] + 200
13  UIKit                               0x002e86f4 -[UINavigationBar setItems:] + 48
14  MyApp                               0x00007dfd -[DashboardViewController viewDidLoad] + 621

显然,标签似乎无法解决其界限,但应该如何解决呢?谢谢 :)

4

1 回答 1

3

您正在UINavigationBar使用 的帧值初始化CGRectNull。尝试CGRectZero改用。

于 2013-09-10T12:12:20.400 回答