以往弄得navigationItem.titleView都是居中在导航栏里,不能左右控制位置,今天得到了下面这种效果,实乃美滋滋。
// 导航栏里面的搜索框
UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
self.navigationItem.titleView = vv;
search = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
[vvaddSubview:search];
======================================================================
如果不设置vv的话,会是这样(好短哦)
// 导航栏里面的搜索框
search = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
self.navigationItem.titleView = search;
//==================================================
过了几天后发现,上面的方法不凑效,问题在点进子页面后,titleView的frame又被更改了。
解决:
// 导航栏里面的搜索框
search = [[SearchView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
search.field.delegate = self;
search.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
self.navigationItem.titleView = search;
// 自定义的titleView,重写CGSizeintrinsicContentSize是关键
#import
@interfaceSearchView :UIView
@property (weak, nonatomic) IBOutlet UITextField *field;
@property(nonatomic,assign)CGSizeintrinsicContentSize;
@end
改成这样之后,很nice
网友评论