iOS 11 navigationItem.titleView Width Not Set

后端 未结 14 1728
遥遥无期
遥遥无期 2020-12-13 07:58

Seeing a behavior on iOS11 with a navigationItem.titleView where the width of the titleView is not the full width of the screen.

I have a custom view that I set as t

14条回答
  •  粉色の甜心
    2020-12-13 08:42

    I had to fit an UIImageView as navigationItem.titleView. The aspect ratio did fit but the intrinsicContentSize made it to large. Scaling the image led to poor image quality. Setting layout anchors worked for me:

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];
    [imageView setImage:image];
    [imageView.widthAnchor constraintEqualToConstant:80].active = YES;
    [imageView.heightAnchor constraintEqualToConstant:30].active = YES;
    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    self.navigationItem.titleView = imageView;
    

提交回复
热议问题