UITableView shifts down ~ 1 inch unintentionally when used using sidebar menus

I have a strange error in my application that uses a sidebar menu with buttons UITableViewto display. When I run my application, everything looks fine and is in the right place .

As soon as I make a selection for the corresponding viewController, and navigate back to sidebar menu theUITableView, it’s shifted by about 1 inch .

If I press the sidebar menu button to close it, the menu is reset and everything works and looks fine. This error occurs only once, as soon as I select a line, return to it, close it, it UITableViewwill be in the correct position until the user restarts the application. Any idea what is 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.

In addition, I tested other objects, such as buttons and labels, and they do not move, but only shift UITableView.

Thank!

+4
source share
3 answers

I have found a solution! This is due to the configuration of the navigation controller. Credit is here.

, UINavigationBar?

, "" .

+3

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); viewDidLoad. , .

+2
  • Try installing self.automaticallyAdjustsScrollViewInsets = NOin your controller.
  • Try installing edgesForExtendedLayouton UIRectEdgeNone.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1541171/


All Articles