简体   繁体   English

旋转取消按钮后,TableViewController UISearchBar不可单击

[英]TableViewController UISearchBar after rotation cancel button not clickable

I got a TableVieController with a searchBar positioned at self.tableView.tableHeaderView . 我有一个TableVieController,它的searchBar定位在self.tableView.tableHeaderView上 When the screen rotates I am just setting the frame to the new CGRect. 屏幕旋转时,我只是将框架设置为新的CGRect。 The view looks alright but unfortunately half of the search bar isn't clickable, neither is the cancel button. 该视图看起来不错,但不幸的是,搜索栏的一半不可单击,取消按钮也不可单击。

So how do I make this work? 那我该如何做呢? I only managed to do it by creating a new instance of the search bar and assign it, but that won't be the right way, right? 我只能通过创建搜索栏的新实例并分配它来做到这一点,但这不是正确的方法,对吗?

const CGRect CGRECT_TABLE_VIEW_PORTRAIT = { { 0.0f, 0.0f }, { 320.0f, 365.0f } };
const CGRect CGRECT_TABLE_VIEW_LANDSCAPE = { { 0.0f, 0.0f }, { 480.0f, 150.0f } };


- (void)viewDidLoad
{
... 
        // create search bar
        searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
        [searchBar setTintColor:RGB(168, 212, 255)] ;    
        searchBar.showsCancelButton = YES;
        [searchBar sizeToFit]; // Get the default height for a search bar.
        searchBar.delegate = self;

        self.tableView.tableHeaderView = searchBar;
        self.tableView.frame = CGRECT_TABLE_VIEW_PORTRAIT;
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration {

        if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
              tableView.frame = CGRECT_TABLE_VIEW_LANDSCAPE;
        } else {
              tableView.frame = CGRECT_TABLE_VIEW_LANDSCAPE;
        }
}

I don't know why you're trying to deal with rotations, when this should happen automatically. 我不知道您为什么要尝试轮换处理,这种情况应该自动发生。 This code worked to give a search bar that adjusts itself on rotation: 这段代码的作用是提供一个可以根据旋转程度进行调整的搜索栏:

- (void)viewDidLoad {
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:self.tableView.bounds];
    [searchBar setTintColor:[UIColor colorWithRed:168/255 green:212/255 blue:1 alpha:1]];
    searchBar.showsCancelButton = YES;
    [searchBar sizeToFit]; // Get the default height for a search bar.
    searchBar.delegate = self;
    self.tableView.tableHeaderView = searchBar;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM