Ios UIImageView块带有自动布局的UINavigationBar

Ios UIImageView块带有自动布局的UINavigationBar,ios,objective-c,autolayout,ios-autolayout,Ios,Objective C,Autolayout,Ios Autolayout,我正在尝试创建一个UIViewController,它有一个UINavigationBar和一个UIImageView。我放在UIImageView中的图像大小是416×901像素。我正在尝试创建一个视图,这样我就可以将导航栏放在顶部,然后将UIImageView固定到屏幕边缘,并放在工具栏下面。然而,当我运行我的应用程序时,UIImage似乎超出了导航栏 以下是Xcode的屏幕截图: 如您所见,UIAvigationBar被UIImage遮挡。当我删除图像时,布局看起来与我预期的一样(我注

我正在尝试创建一个UIViewController,它有一个UINavigationBar和一个UIImageView。我放在UIImageView中的图像大小是
416×901像素
。我正在尝试创建一个视图,这样我就可以将导航栏放在顶部,然后将UIImageView固定到屏幕边缘,并放在工具栏下面。然而,当我运行我的应用程序时,UIImage似乎超出了导航栏

以下是Xcode的屏幕截图:

如您所见,UIAvigationBar被UIImage遮挡。当我删除图像时,布局看起来与我预期的一样(我注释掉UIImage分配
image.image=[UIImage ImageName:@“attachment.jpg”];
,您可以在下面我发布的代码中看到):

我被难住了。我猜这与UIImageView中缺少的参数有关,或者我需要创建一个完全不同的约束,但我不确定。我已经为下面的布局粘贴了代码。我很感激能得到的任何帮助

@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
UINavigationBar*导航=[[UINavigationBar alloc]initWithFrame:CGRectZero];
navigation.translatesAutoresizingMaskIntoConstraints=否;
UINavigationItem*navigationItem=[[UINavigationItem alloc]initWithTitle:@“编辑”];
UIBarButtonItem*cancelBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel目标:自我操作:@selector(取消:)];
navigationItem.leftBarButtonItem=cancelBtn;
UIBarButtonItem*doneBtn=[[UIBarButtonItem alloc]initWithTitle:@“保存”样式:UIBarButtonItemStyleDone目标:自我操作:@selector(保存:)];
navigationItem.RightBarButtonim=doneBtn;
[navigation setItems:@[navigationItem]];
navigation.delegate=self;
UIImageView*image=[[UIImageView alloc]init];
image.contentMode=UIViewContentModeScaleAspectFit;
image.backgroundColor=[UIColor redColor];
image.image=[UIImage ImageName:@“attachment.jpg”];
image.translatesAutoResizengMaskintoConstraints=否;
UIView*content=[[UIView alloc]initWithFrame:CGRectZero];
content.translatesAutoresizingMaskIntoConstraints=否;
content.backgroundColor=[UIColor yellowColor];
[内容添加子视图:图像];
[content addConstraints:[NSLayoutConstraint Constraints WithVisualFormat:@“V:|-0-[image]-0-|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(图像)];
[content addConstraints:[NSLayoutConstraint Constraints WithVisualFormat:@“H:|-0-[image]-0-|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(图像)];
[self.view addSubview:导航];
[self.view addSubview:content];
[self.view addConstraints:[nsLayoutConstraints-WithVisualFormat:@“V:|-[navigation]-0-[content]-0-|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(导航、内容)];
[self.view addConstraints:[NSLayoutConstraints Constraints WithVisualFormat:@“H:|-0-[navigation]-0-|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(导航、内容)];
[self.view addConstraints:[nsLayoutConstraints Constraints WithVisualFormat:@“H:|-0-[content]-0-|”
选项:0
指标:零
视图:NSDictionaryOfVariableBindings(导航、内容)];
}
-(UIBAR位置)杆的位置:(id)杆{
返回UIBarPositionTopAttached;
}
-(作废)取消:(id)按钮{}
-(void)保存:(id)按钮{}
@结束
我刚刚注意到Xcode中有一些关于布局的运行时警告。以下是Xcode向我展示的内容:


我猜导航栏的高度是不明确的,因为UIImageView的高度没有很好的定义。然而,在本例中,我试图固定图像的高度,期望导航的高度得到很好的定义。基本上,我依靠导航来定义UIImageView需要多少空间。也许我需要固定两个元素之一的高度?

添加自己的导航栏有点奇怪,因为当设备旋转时需要调整大小

像这样尝试(我不喜欢VFL-有时似乎有自己的问题):

@界面视图控制器()
@属性(强,非原子)NSLayoutConstraint*navBarHeightCon;
@结束
@实现视图控制器
-(无效)视图将布局子视图{
[超级视图将布局子视图];
_navBarHeightCon.constant=(self.view.bounds.size.height>self.view.bounds.size.width)?44:32;
}
-(无效)viewDidLoad{
[超级视图下载];
UINavigationBar*导航=[[UINavigationBar alloc]initWithFrame:CGRectZero];
navigation.translatesAutoresizingMaskIntoConstraints=否;
UINavigationItem*navigationItem=[[UINavigationItem alloc]initWithTitle:@“编辑”];
UIBarButtonItem*cancelBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel目标:自我操作:@选择器(取消:
@interface ViewController ()
@property (strong, nonatomic) NSLayoutConstraint *navBarHeightCon;
@end

@implementation ViewController

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    _navBarHeightCon.constant = (self.view.bounds.size.height > self.view.bounds.size.width)? 44 : 32;
}

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

    [self.view addSubview:navigation];
    [self.view addSubview:content];

    UILayoutGuide *g = self.view.safeAreaLayoutGuide;

    _navBarHeightCon = [navigation.heightAnchor constraintEqualToConstant:44.0];

    [NSLayoutConstraint activateConstraints:@[

        // constrain content view top to navigation bar bottom
        [content.topAnchor constraintEqualToAnchor:navigation.bottomAnchor],

        // constrain content view bottom / leading / trailing to safe area
        [content.bottomAnchor constraintEqualToAnchor:g.bottomAnchor],
        [content.leadingAnchor constraintEqualToAnchor:g.leadingAnchor],
        [content.trailingAnchor constraintEqualToAnchor:g.trailingAnchor],

        // constrain image top / bottom / leading / trailing to content view
        [image.topAnchor constraintEqualToAnchor:content.topAnchor],
        [image.bottomAnchor constraintEqualToAnchor:content.bottomAnchor],
        [image.leadingAnchor constraintEqualToAnchor:content.leadingAnchor],
        [image.trailingAnchor constraintEqualToAnchor:content.trailingAnchor],

        // constrain navigation bar top / leading / trailing to safe area
        [navigation.topAnchor constraintEqualToAnchor:g.topAnchor],
        [navigation.leadingAnchor constraintEqualToAnchor:g.leadingAnchor],
        [navigation.trailingAnchor constraintEqualToAnchor:g.trailingAnchor],

        // height constraint will be set in viewWillLayoutSubviews
        _navBarHeightCon,

    ]];

}

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}

- (void)cancel:(id)button {}
- (void)save:(id)button {}

@end