Search code examples
uisplitviewcontrolleripad

SplitViewController BUG?


I set up a very simple iOS splitView Controller project that has a very weird bug. If you start the app in landscape mode, click on a button to open another view, rotate to portrait, and then return to starting view - the table menu on the left should not be visible but it is.. Weirdly enough you can't interact with it, which leads me to believe its not really there but some sort of artifact.

Note, this only happens the first time you do it and then never again, until you start the app over in landscape.

MasterViewController:

#import "MasterViewController.h"

#import "DetailViewController.h"


@interface MasterViewController () {
    NSMutableArray *_objects;
}

@property (nonatomic, strong)UITableView *menu;

@end

@implementation MasterViewController

- (void)awakeFromNib
{
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    [super awakeFromNib];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

}


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

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDate *object = _objects[indexPath.row];
    self.detailViewController.detailItem = object;
}

@end

DetailViewController:

#import "DetailViewController.h"
#import "StandardViewViewController.h"


@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;

-(IBAction)buttonTapped:(id)sender;

- (void)configureView;
@end

@implementation DetailViewController

#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }

    if (self.masterPopoverController != nil) {
        [self.masterPopoverController dismissPopoverAnimated:YES];
    }        
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
    }
}

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

    [self configureView];
}

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

#pragma mark - Split view

- (void)splitViewController:(UISplitViewController *)splitController willHideViewController:(UIViewController *)viewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)popoverController
{
    barButtonItem.title = NSLocalizedString(@"Master", @"Master");
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.masterPopoverController = popoverController;
}

- (void)splitViewController:(UISplitViewController *)splitController willShowViewController:(UIViewController *)viewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    // Called when the view is shown again in the split view, invalidating the button and popover controller.
    [self.navigationItem setLeftBarButtonItem:nil animated:YES];
    self.masterPopoverController = nil;
}

-(IBAction)buttonTapped:(id)sender
{
    StandardViewViewController *sv = [[StandardViewViewController alloc]initWithNibName:@"StandardViewViewController" bundle:nil];
    [self.splitViewController presentViewController:sv animated:YES completion:nil];

}

@end

Is this a known bug? is there a solution?


Solution

  • Altough not pretty, the solution suggested here works. This is how you would implement the solution:

    -(IBAction)buttonTapped:(id)sender
    {
        StandardViewViewController *sv = [[StandardViewViewController alloc] initWithNibName:nil bundle:nil];
        sv.modalPresentationStyle = UIModalPresentationPageSheet; // <-- Add this
        [self.splitViewController presentViewController:sv animated:YES completion:nil];
    }
    

    In the StandardViewController.m add this:

    -(void)viewWillLayoutSubviews {
        self.view.bounds = [StandardViewViewController screenBoundsForCurrentOrientation];
        [super viewWillLayoutSubviews];
    }
    
    +(CGRect)screenBoundsForCurrentOrientation {
        return [self screenBoundsForOrientation:[UIApplication sharedApplication].statusBarOrientation];
    }
    
    +(CGRect)screenBoundsForOrientation:(UIInterfaceOrientation)orientation {
    
        UIScreen *screen = [UIScreen mainScreen];
        CGRect fullScreenRect = screen.bounds; //implicitly in Portrait orientation.
    
        if(orientation == UIInterfaceOrientationLandscapeRight || orientation ==  UIInterfaceOrientationLandscapeLeft){
            CGRect temp = CGRectZero;
            temp.size.width = fullScreenRect.size.height;
            temp.size.height = fullScreenRect.size.width;
            fullScreenRect = temp;
        }
    
        return fullScreenRect;
    }
    

    To make it prettier you could put the code for the StandardViewController into a new class that the StandardViewController could extend on.

    And, yes, this looks like a bug.