重写titleView

时间:2022-10-15 00:22:12

在一些特定的情况下不能使用原有的titleView需要重写titleView代码如下

#import "TitleView.h"

@implementation TitleView

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(, , kWindowW - , )];
        [self addSubview:self.titleLabel];
    }
    return self;
}

- (void)setFrame:(CGRect)frame {

    [super setFrame:CGRectMake(, , self.superview.frame.size.width - , self.superview.bounds.size.height)];

}
@end

引入写成属性后调用

self.navigationItem.titleView = _titleView;

效果如下

重写titleView