简体   繁体   中英

TableViewController UISearchBar after rotation cancel button not clickable

I got a TableVieController with a searchBar positioned at self.tableView.tableHeaderView . When the screen rotates I am just setting the frame to the new 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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