iOS 11 navigationItem.titleView Width Not Set

后端 未结 14 1763
遥遥无期
遥遥无期 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:49

    When you have a UIView as subview inside CustomTitleView, intrinsicContentSize solution does not work, for me in XCODE 9 in iOS 11 only. so I did like below, works fine for me, might this help someone.

    @interface CustomTitleView : UIView
    @property (weak, nonatomic) IBOutlet UIView *doubleTitleView;
    @end
    
    @implementation CustomTitleView
    - (void)awakeFromNib {
        [super awakeFromNib];
        int width = _doubleTitleView.frame.size.width;
        int height = _doubleTitleView.frame.size.height;
        if (width != 0 && height != 0) {
    
            NSLayoutConstraint *widthConstraint =  [_doubleTitleView.widthAnchor constraintEqualToConstant:width];
            NSLayoutConstraint *heightConstraint = [_doubleTitleView.heightAnchor constraintEqualToConstant:height];
    
            [_doubleTitleView addConstraint:heightConstraint];
            [_doubleTitleView addConstraint:widthConstraint];
            [heightConstraint setActive:TRUE];
            [widthConstraint setActive:TRUE];
        }
    }
    

提交回复
热议问题