简体   繁体   中英

iOS 11 custom titleView size; autolayout seems to be ignored

I have a uinavigationbar as part of a navigationcontroller that I need to be 70 pixels high. Pre-iOS11 there are various solutions and I implemented one.

During the iOS 11 beta, I tried various ways of using autolayout to size the titleView. According to the WWDC talk, it should recognize width+height constraints within the titleview and accommodate them. It does not appear to do so.

It looks like it conflicts with the built in UILayoutGuide

(
    "<NSLayoutConstraint:0x60000008c760 UILayoutGuide:0x6000001acbe0'TitleView(0x7fe600c0a710)'.height == 44   (active)>",
    "<NSLayoutConstraint:0x61400008c6c0 EasyRelease.CustomTitleView:0x7fe606008e70.top >= UILayoutGuide:0x6000001acbe0'TitleView(0x7fe600c0a710)'.top   (active)>",
    "<NSLayoutConstraint:0x61400008c800 EasyRelease.CustomTitleView:0x7fe606008e70.centerY == UILayoutGuide:0x6000001acbe0'TitleView(0x7fe600c0a710)'.centerY   (active)>",
    "<NSLayoutConstraint:0x61400008bbd0 EasyRelease.CustomTitleView:0x7fe606008e70.height == 70   (active)>"
)

What gives? Is this feature not implemented correctly in iOS11, or am I missing something? Are there any known workarounds for adjusting titleview+uinavigationbar height?

edit: The way my code works, is I have added width + height constraints to the titleView. Is there anything else I should do? From the WWDC presentation this looked sufficient.

Take a look at the answer's here: iOS 11 navigationItem.titleView Width Not Set

I had a similar problem and overriding "intrinsicContentSize" lead me to the fix.

This could work, it put a view above the NavigationBar. You should note that your image should be 70 pixels height. 13 is a magic number for 70 pixels image, because it appears centered in the navigation bar.

-(void)updateNavBarBackground{
    [[self.navigationController.navigationBar viewWithTag:1] removeFromSuperview];
    UIImage * image = [ UIImage imageNamed:@"NavBarImage"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.center = CGPointMake(self.navigationController.navigationBar.frame.size.width  / 2,
                               self.navigationController.navigationBar.frame.size.height / 2 - 13);

    imageView.tag = 1;
    [self.navigationController.navigationBar addSubview : imageView];
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM