【发布时间】:2012-12-13 19:31:58
【问题描述】:
我有一个UIToolbar,里面有 5 个项目:
-
UIBarButtonItem带图片 -
UIBarButtonItem伸缩宽度 -
UIBarButtonItem带有自定义视图 (UISearchBar) -
UIBarButtonItem伸缩宽度 -
UIBarButtonItem带图片
当我选择UISearchBar 时,我让它保持正确的大小并变为活动状态。但是,我希望左侧和右侧的图像消失,并为UISearchBar 提供UIToolbar 的全宽。我该怎么做?
这就是我所拥有的: https://dl.dropbox.com/u/3077127/demo_1.mov
这就是我想要的: https://dl.dropbox.com/u/3077127/demo_2.mov
这是我的代码:
#pragma mark View lifecycle
- (void)viewDidLoad {
[...]
SearchBar *mySearchBar = [[SearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar setTintColor:[UIHelpers getSlColor]];
CGRect searchBarFrame = mySearchBar.frame;
searchBarFrame.size.width = 218;
[mySearchBar setFrame:searchBarFrame];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:mySearchBar.frame];
[searchBarContainer addSubview:mySearchBar];
[searchBarContainer setTag:99];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
NSArray *items = [[NSArray alloc] initWithObjects:left, flex, search, flex, right, nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolbar setItems:items];
[toolbar setTintColor:[UIHelpers getSlColor]];
self.tableView.tableHeaderView = toolbar;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectNull];
[...]
[super viewDidLoad];
}
#pragma mark -
【问题讨论】:
-
嘿,在这里查看我的 SO 答案link,这与您尝试做的类似。
-
这似乎有效。但是,我不使用界面生成器,而是通过代码完成所有操作......这也应该可以。将您的回复添加为答案,我会批准。
-
很高兴能提供帮助 :) 将其作为答案发布。
标签: objective-c ios uiview uisearchbar uitoolbar