gpt4 book ai didi

ios6 - iOS UINavigationBar 因自动布局而崩溃

转载 作者:行者123 更新时间:2023-12-04 04:42:18 26 4
gpt4 key购买 nike

我有一个带有一个 UINavigationBar 和一个 UIView 的 View Controller 。我曾尝试使用自动布局,但是一旦我向 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

显然,标签似乎无法解决其边界问题,但这应该如何解决?谢谢:)

最佳答案

您正在初始化 UINavigationBar帧值为 CGRectNull .尝试使用 CGRectZero相反。

关于ios6 - iOS UINavigationBar 因自动布局而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18716611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com