objective-c - 更改表格 View 中搜索栏的宽度

标签 objective-c ios ios5 uisearchbar

我意识到会有评论,例如这是重复的:

Change the Width of UISearchBars on a TableView

我认为不是。为什么我无法更改 Storyboard 中的宽度?

enter image description here

我可以在宽度字段中写入任何值,但是一旦我按下 return/enter 键,值 320 就会恢复。

我也尝试过在 view did load 方法中执行此操作:

searchBar.frame = CGRectMake(0, 0, 222, 45);

再次成功为零,有谁知道如何做到这一点?我有一个索引表,右侧的字母与搜索文本字段重叠,看起来非常难看。

更新

简单来说,这就是我想要的(概念上):

enter image description here

这是我设法制作的:

enter image description here

让我再次重申这个问题“我正在尝试调整搜索栏的大小,以便字母不会与其重叠”。

我尝试过 taus-iDeveloper 建议,它确实调整了大小,但表格单元格/部分与其重叠。

也尝试过这个:

Changing the size of the UISearchBar TextField?

我设法在 Storyboard中调整它的大小,但当应用程序运行时,大小没有改变。

enter image description here

我完全不知道下一步该尝试什么..

更新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/

相关文章:

ios - 如果我正在执行 CGImageRelease(sourceImageRef), Objective-C ,应用程序崩溃

objective-c - 每当我需要操作业务对象时,直接使用核心数据是一种好的做法吗?

ios - 使用属性动态初始化任何 Objective-C 类

objective-c - 如何更改 Swift 类的命名空间?

ios - CLLocationManager() Location 在模拟器中为 nil

ios - 将文档目录的路径传递给 xcode 运行脚本

iphone - 使用 iOS5 的 XCode 编译现有的 Lua 应用程序

ios - 自动布局,UiScrollView 内的 UITableView 不会增长

ios - 核心数据关系问题

objective-c - 如何通过点击动画 UIImageview 以显示全屏?