我意识到会有评论,例如这是重复的:
Change the Width of UISearchBars on a TableView
我认为不是。为什么我无法更改 Storyboard 中的宽度?
我可以在宽度字段中写入任何值,但是一旦我按下 return/enter 键,值 320 就会恢复。
我也尝试过在 view did load 方法中执行此操作:
searchBar.frame = CGRectMake(0, 0, 222, 45);
再次成功为零,有谁知道如何做到这一点?我有一个索引表,右侧的字母与搜索文本字段重叠,看起来非常难看。
更新
简单来说,这就是我想要的(概念上):
这是我设法制作的:
让我再次重申这个问题“我正在尝试调整搜索栏的大小,以便字母不会与其重叠”。
我尝试过 taus-iDeveloper 建议,它确实调整了大小,但表格单元格/部分与其重叠。
也尝试过这个:
Changing the size of the UISearchBar TextField?
我设法在 Storyboard中调整它的大小,但当应用程序运行时,大小没有改变。
我完全不知道下一步该尝试什么..
更新2
我接受了答案,因为它有一些解决方案的元素,我得出的结论是,这不能仅使用 Storyboard以编程方式完成,解决方案:
//Add the search bar uiview wrapper
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.frame.size.width, 44.0)];
//Add the search bar and navigation item to fill in the remaining space
UINavigationBar *remainingSearchSpace = [[UINavigationBar alloc] initWithFrame:CGRectMake(290, 0.0, (self.tableView.frame.size.width-290), 44.0)];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 290, 44.0)];
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
[searchBarView addSubview:remainingSearchSpace];
self.tableView.tableHeaderView = searchBarView;
最佳答案
试试这个: 在.h文件中,
@interface SearchBarController : UIViewController <UISearchBarDelegate>
@property (nonatomic, retain) UISearchBar *mySearchBar;
在.m文件中,
@synthesize mySearchBar;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(@"SearchBarTitle", @"");
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; // use the table view background color
self.mySearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0)] autorelease];
self.mySearchBar.delegate = self;
self.mySearchBar.showsCancelButton = YES;
self.mySearchBar.showsBookmarkButton = YES;
[self.view addSubview: self.mySearchBar];
// provide tint coloring and background image only if its available
if (![self.mySearchBar respondsToSelector:@selector(setSearchFieldBackgroundImage:forState:)])
{
// hide the segmented control allowing for various content options
self.contentOptions.hidden = YES;
}
}
希望这有帮助!!
关于objective-c - 更改表格 View 中搜索栏的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914817/