简体   繁体   中英

Set a UIPickerView to bottom of UITableview regardless of scroll position

I'd like to present a UIPickerView snapped to the bottom of a UITableView regardless of where it is scrolled.

Currently I have a UIPickerView added to a UITableView that I present when a button is pressed, but when I scroll the table the UIPickerView goes out of view, and if I'm scrolled out of range of where I've presented it, the UIPickerView appears to have never been called.

Does anyone know how to accomplish this?

Thank

The use of UITableViewController is great unless you need to add subview that don't scroll with the table view. It can't really be done. The solution is to use a UIViewController instead and add your own table view, setup the table view dataSource and delegate protocols and replicate the basic table view controller plumbing.

Once your view controller works like a normal table view controller again, you can now add subviews to the view controller's view that won't scroll with the table.

if u add tableview programatically

    SearchtableView.frame = CGRectMake(450, 90, 318, 600);
            SearchtableView  =  [[UITableView alloc] initWithFrame:CGRectMake(623, 90, 400, 400)style:UITableViewStyleGrouped];
            [self.view addSubview:SearchtableView];
//belove the tableview//
//Add UIPickerView to the self.view]//

Something told me that there must be a simpler alternative to the answers that I'd been given, and I found a solution.

I decided to find the y coordinate of the scroll point using scrollViewDidScroll: and then animating the UIPickerView to the desired location.

    ...
    [UIView beginAnimations:nil context:NULL];
     CGFloat pointY = self.scrollPoint.y;
    [self.sortPicker setFrame:CGRectMake(0, pointY + 200, 320, 216)];
    [UIView commitAnimations];
    ...

The UIPickerView will only appear if the scroll has been stopped so it's necessary to implement a method to stop the scroll on touch:

- (void)stopScroll:(UITableView *)tableview
{
    CGPoint offset = tableview.contentOffset;
    [tableview setContentOffset:offset animated:NO];
}

This will allow you to display and dismiss the UIPickerView at any moment at any given destination.

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