Question

Every time I click on my UISegementedControl it snaps back to its original frame. I can see it barely through my translucent toolbar. I have a UIViewController with a UITableView, and UIToolBar like this: my view controller

There is a UISegmentedControl hidden just below the table view, behind the toolbar: storyboard layout setup

The Filter button calls the 'onFilterButtonPressed' method

- (IBAction)onFilterButtonPressed:(id)sender
{
    if(self.filterBar.hidden){
        [self showFilterBar];
    } else {
        [self hideFilterBar];
    }
}

- (void)hideFilterBar
{
    CGRect filterBarFrame = CGRectMake(0, self.view.frame.size.height+(self.filterBar.frame.size.height+1), self.filterBar.frame.size.width, self.filterBar.frame.size.height);
    CGRect tableViewFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y,self.tableView.frame.size.width, self.tableView.frame.size.height+(self.filterBar.frame.size.height+1));

    [UIView animateWithDuration:0.3 animations:^{
        [self.filterBar setFrame:filterBarFrame];
        [self.tableView setFrame:tableViewFrame];
    } completion:^(BOOL finished) {
        self.filterBar.hidden = YES;
    }];
}

- (void)showFilterBar
{
    CGRect filterBarFrame = CGRectMake(0, self.view.frame.size.height-(self.filterBar.frame.size.height+1), self.filterBar.frame.size.width, self.filterBar.frame.size.height);
    CGRect tableViewFrame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y,self.tableView.frame.size.width, self.tableView.frame.size.height-(self.filterBar.frame.size.height+1));

    self.filterBar.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        [self.tableView setFrame:tableViewFrame];
        [self.filterBar setFrame:filterBarFrame];
    }];

}
Was it helpful?

Solution

This is because of auto layout. With that turned on (which it is by default), you should do any positioning or resizing of views by modifying constraints, not setting frames.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top