【发布时间】:2020-06-20 18:03:23
【问题描述】:
我正在尝试创建一个具有 UINavigationBar 和 UIImageView 的 UIViewController。我放入 UIImageView 的图像大小为416 × 901 pixels。我正在尝试创建一个视图,使导航栏位于顶部,然后 UIImageView 固定在屏幕边缘和工具栏下方。但是,当我运行我的应用程序时,UIImage 似乎超出了导航栏。
这是来自 Xcode 的截图:
如您所见,UINavigationBar 被 UIImage 遮挡。当我删除图像时,布局看起来像我预期的那样(我注释掉 UIImage 分配image.image = [UIImage imageNamed:@"attachment.jpg"];,您可以在下面发布的代码中看到):
我很困惑。我猜它与 UIImageView 中缺少的参数有关,或者我需要创建一个完全不同的约束,但我不确定。我已经粘贴了下面布局的代码。我很感激我能得到的任何帮助。
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navigation = [[UINavigationBar alloc] initWithFrame:CGRectZero];
navigation.translatesAutoresizingMaskIntoConstraints = NO;
UINavigationItem* navigationItem = [[UINavigationItem alloc] initWithTitle:@"Edit"];
UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
navigationItem.leftBarButtonItem = cancelBtn;
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
navigationItem.rightBarButtonItem = doneBtn;
[navigation setItems:@[navigationItem]];
navigation.delegate = self;
UIImageView *image = [[UIImageView alloc] init];
image.contentMode = UIViewContentModeScaleAspectFit;
image.backgroundColor = [UIColor redColor];
image.image = [UIImage imageNamed:@"attachment.jpg"];
image.translatesAutoresizingMaskIntoConstraints = NO;
UIView *content = [[UIView alloc] initWithFrame:CGRectZero];
content.translatesAutoresizingMaskIntoConstraints = NO;
content.backgroundColor = [UIColor yellowColor];
[content addSubview:image];
[content addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[image]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(image)]];
[content addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[image]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(image)]];
[self.view addSubview:navigation];
[self.view addSubview:content];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[navigation]-0-[content]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(navigation, content)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[navigation]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(navigation, content)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[content]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(navigation, content)]];
}
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}
- (void)cancel:(id)button {}
- (void)save:(id)button {}
@end
我刚刚注意到 Xcode 中有一些关于布局的运行时警告。这是 Xcode 向我展示的内容:
我猜导航栏的高度是模棱两可的,因为 UIImageView 的高度没有很好的定义。但是在这种情况下,我试图固定图像的高度,期望导航的高度能够很好地定义。基本上,我依靠导航来定义 UIImageView 需要多少空间。也许我需要修复两个元素之一的高度?
【问题讨论】:
标签: ios objective-c autolayout ios-autolayout