前言
下面的代碼沪么,定義了titleView的寬度為屏幕寬度锌半。
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
titleView.backgroundColor = [UIColor redColor];
self.navigationItem.titleView = titleView;
實際效果:
效果并不理想刊殉。
解決方案
一、創(chuàng)建個UIView類:
@interface CustomTitleView : UIView
@end
@implementation CustomTitleView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)setFrame:(CGRect)frame {
[super setFrame:CGRectMake(0, 0, self.superview.bounds.size.width, self.superview.bounds.size.height)];
}
@end
二逸月、調用
UIView *titleView = [[CustomTitleView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
titleView.backgroundColor = [UIColor greenColor];
self.navigationItem.titleView = titleView;