Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView is shifting down ~1inch unintentionally when using using sidebar menu

I'm having a weird bug in my app that uses a sidebar menu with a UITableView to list buttons to navigate. When I start my app, everything looks fine and is in the correct place.

Once I make a selection to the respective viewController, and navigate back to sidebar menu theUITableView` is shifted down about 1 inch.

If I press the sidebar menu button to close it, the menu resets and everything works and looks normal. This bug only happens once, once I select an row, navigate back to it, close it, the UITableView will be in the correct position until the user hard restarts the app. Any idea whats going on?

Here is my code for the sidebar menu:

#import "SideBarViewController.h"

@interface SideBarViewController ()

@end

@implementation SideBarViewController

@synthesize controllerArray, sidebarButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    controllerArray = [NSArray arrayWithObjects:@"Search For Games", @"Login", @"Library", nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [controllerArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [controllerArray objectAtIndex:indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UIViewController *selection;

    if (indexPath.row == 0) {
        selection = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchController"];
        [self.navigationController pushViewController:selection animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else if (indexPath.row == 1) {
        selection = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginController"];
        [self.navigationController pushViewController:selection animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else if (indexPath.row == 2) {
        selection = [self.storyboard instantiateViewControllerWithIdentifier:@"LibraryController"];
        [self.navigationController pushViewController:selection animated:YES];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }

}

I followed the tutorial on the "Ray Wenderlich" website for sidebar code.

Also, I've tested other objects, such as buttons and labels and they do not move, its only the UITableView that shifts.

Thanks!

like image 209
DookieMan Avatar asked May 19 '14 19:05

DookieMan


People also ask

Why is there extra padding at the top of my UITableView?

Starting in iOS7, there is additional space at the top of my UITableView 's which have a style UITableViewStyleGrouped . The tableview starts at the first arrow, there are 35 pixels of unexplained padding, then the green header is a UIView returned by viewForHeaderInSection (where the section is 0).

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


3 Answers

I found the solution! It had to do with the setting for the Navigation controller. Credit due here.

Container View getting pushed down as if it had a UINavigationBar?

You need to check and see if "Translucent" is checked under the Navigation Bar settings.

like image 89
DookieMan Avatar answered Dec 06 '22 18:12

DookieMan


Try adding self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); to viewDidLoad. This could be a solution if your app is adding insets automatically.

like image 44
Eichhörnchen Avatar answered Dec 06 '22 19:12

Eichhörnchen


  1. Try setting self.automaticallyAdjustsScrollViewInsets = NO in your view controller.
  2. Try setting the edgesForExtendedLayout to UIRectEdgeNone.

Hope it helps.

like image 34
Rafa de King Avatar answered Dec 06 '22 18:12

Rafa de King