iOS 11 navigationItem.titleView Width Not Set

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

    Using @falkon's answer here's the code:

    Add this code to the view that is used as titleView

    override var intrinsicContentSize: CGSize {
        return UILayoutFittingExpandedSize
    } 
    
    0 讨论(0)
  • 2020-12-13 08:59

    The most important is that you need overwrite customTitleView as your titleView:

    self.navigationItem.titleView = [self titleView];

    #pragma mark - getter
    
    - (UIView *)titleView {
        UIView *navTitleView = [HFCalenderTitleView new];
        navTitleView.frame = CGRectMake(0.0, 0.0, HorizontalFrom750(200.0), 44.0);
    
        [navTitleView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(navTitleView);
        }];
    
        CGFloat btnWidth = 30.0;
        [navTitleView addSubview:self.previousButton];
        self.previousButton.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 15.0);
        [self.previousButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(navTitleView);
            make.top.bottom.equalTo(navTitleView);
            make.width.equalTo(@(btnWidth));
        }];
        [navTitleView addSubview:self.nextBtn];
        self.nextBtn.imageEdgeInsets = UIEdgeInsetsMake(0.0, 15.0, 0.0, 0.0);
        [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(navTitleView);
            make.top.bottom.equalTo(navTitleView);
            make.width.equalTo(@(btnWidth));
        }];
    
        return navTitleView;
    }
    #pragma mark - customTitleView
    
    #import "HFCalenderTitleView.h"
    @implementation HFCalenderTitleView
    - (CGSize)intrinsicContentSize{
        return CGSizeMake(HorizontalFrom750(200.0), 40); // the target size
    }
    

    0 讨论(0)
提交回复
热议问题