ABC
ABC

Reputation: 718

UITableView not autosizing to fit its UIView superview

I am trying to make the height of a UITableView smaller by changing the size of its parent, a UIView.

I cannot directly change the UITableView size, just the UIView due to the way my project is set up.

The UIView changes its size and origin, which moves the table down fine, but this means that the bottom of the table is cut off. I have to do this after the table has been drawn and using layoutIfNeeded makes the origin (0,0) again.

I have tried to change the autoresizingmasks on both the table and the UIView, but this didn't work. I tried to make the view 100x100 (to test) but the height was still its full size however it made scrolling the table only possible in the 100x100 UIView space.

Any suggestions would be greatly appreciated!

EDIT:

The code I am using is this: contentView is a subview of self.view and the table is a child of this. isTableView is a BOOL which is passed to the object when init, which stops the layout being rendered again. without this, the view resets to its original size as I said before. yaxis is just an extra spacing variable if needed, it just allows more space if needed

UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (ad != nil) {
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            [ad setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierLandscape];
        }else{
            [ad setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifierPortrait];
        }
        
        CGRect bannerViewFrame = ad.frame;
        CGRect contentViewFrame = contentView.frame;
        if (_bannerViewIsVisible) {
            bannerViewFrame.origin.x = 0;
            bannerViewFrame.origin.y = yaxis;
            
            contentViewFrame.size.height = viewController.view.frame.size.height - ([self getBannerHeight]+yaxis);
            contentViewFrame.origin.y = [self getBannerHeight]+yaxis;
            
        }else{
            bannerViewFrame.origin.x = 0;
            bannerViewFrame.origin.y = -[self getBannerHeight];
            
            contentViewFrame.origin.y = yaxis;
            contentViewFrame.size.height = viewController.view.frame.size.height-yaxis;
        }
        [UIView animateWithDuration:[UIView areAnimationsEnabled] ? 0.25 : 0.0 animations:^{
            contentView.frame = contentViewFrame;
            if(!isTableView)
                [contentView layoutIfNeeded];
            ad.frame = bannerViewFrame;
        }];
    }

Upvotes: 1

Views: 2277

Answers (3)

trickster77777
trickster77777

Reputation: 1208

i've solved such a problem by placing dynamic controls and table on separate view. I had different navigation bar height + iAd banner appearing and disappearing.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
  if (!self.bannerIsVisible)
  {
    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    banner.frame = CGRectMake(0, 0, 320, 50);

    self.tableView.frame = CGRectMake(0, 50, 320, self.view.frame.size.height - 50);

    [UIView commitAnimations];
    self.bannerIsVisible = YES;

  }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    banner.frame = CGRectMake(0, 0, 320, 0);

    self.tableView.frame = CGRectMake(0, 0, 320, self.view.frame.size.height);

    [UIView commitAnimations];
    self.bannerIsVisible = NO;
}

Upvotes: 1

wesley
wesley

Reputation: 859

Have you tried to set the frame for UITableview before or after adding the parent UIView as like [myTableView setFrame:CGRectMake(parentView.frame.origin.x+5,parentView.frame.origin.y+5,parentView.frame.size.width-10,parentView.frame.size.height-10)];?

Upvotes: 0

Valentin Shamardin
Valentin Shamardin

Reputation: 3658

yIf you add your UITableView to UIView dynamically, try to set autoresizingMask:

tableView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);

and then add tableView as subView:

[view addSubView:tableView];

hope this helps

Upvotes: 0

Related Questions