Question

In Portrait mode on the iPhone Simulator or in Landscape on the Device, it shows all of the navigation bar buttons, but when used in Portrait mode on the Device, the button is not displayed. Below are images of the navigation bars.

Simulator shows button

Device doesn

The device I have for testing is an iPhone 4S running iOS 6.1.3 (10B329). The simulator I am using is Version 7.0 (463.9.4) running iOS 6.0/6.1.

I am considering removing the Search button while in Edit mode, but I'd prefer to keep this option available to the user regardless of mode.

Any help or insight is appreciated, thanks.

Edit: The right buttons are initially created and added in viewDidLoad: for the ViewController like so:

_deleteBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteRows:)];
_deleteBarButtonItem.tintColor = [UIColor redColor];

_searchBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchButtonClicked:)];

self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, self.editButtonItem];

And when entering edit mode:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (self.tableView.isEditing) {
        // turn off editing
        _deleteBarButtonItem.enabled = NO;
        [self.tableView setEditing:NO animated:animated];
        [self.editButtonItem setStyle:UIBarButtonItemStylePlain];
        self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, self.editButtonItem];
    } else { 
        // turn on editing
        [self.tableView setEditing:YES animated:animated];
        [self.editButtonItem setStyle:UIBarButtonItemStyleDone];
        self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, _deleteBarButtonItem, self.editButtonItem];
    }
}
Was it helpful?

Solution 2

I went with the "less options" option for specifically iPhones in Portrait mode because I couldn't find a proper solution for the disappearing button and there really wasn't enough space. In Landscape and on iPad, the third button, _searchBarButtonItem has room to show, so it is displayed.

Below are the changes I made to get the desired behaviour. I hope some people can find this useful.

-(void) viewDidLoad {
    ...
    self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, self.editButtonItem];
}

- (void) viewWillAppear:(BOOL)animated
{    
    [super viewWillAppear:animated];

    if (self.playerTableView.isEditing && !IS_IPAD)
    {
        if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        {
            self.navigationItem.rightBarButtonItems = @[_deleteBarButtonItem, self.editButtonItem];
        }
        else
        {
            self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, _deleteBarButtonItem, self.editButtonItem];
        }
    }
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (self.playerTableView.isEditing && !IS_IPAD)
    {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
        {
            self.navigationItem.rightBarButtonItems = @[_deleteBarButtonItem, self.editButtonItem];
        }
        else
        {
            self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, _deleteBarButtonItem, self.editButtonItem];
        }
    }
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    ...    
    if (self.playerTableView.isEditing) {
         ...
         self.navigationItem.rightBarButtonItems = @[_filterBarButtonItem, self.editButtonItem];
    } else {
        ...
        if (!IS_IPAD && UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]))
        {
            self.navigationItem.rightBarButtonItems = @[_deleteBarButtonItem, self.editButtonItem];
        }
        else
        {
            self.navigationItem.rightBarButtonItems = @[_searchBarButtonItem, _deleteBarButtonItem, self.editButtonItem];
        }
    }
}

OTHER TIPS

Honestly, thats curious. Perhaps it is some property of the title that makes it dominant. In my experience, it is always best to have less options during "editing mode". If you decide to go that route, here is a little code that might help. (naturally, your variable names most likely differ)

// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];

if (editing) {
    // This is how you remove the button from the toolbar and animate it
    [toolbarButtons removeObject:self.myButton];
    [self setToolbarItems:toolbarButtons animated:YES];
} else {
    // This is how you add the button to the toolbar and animate it
    if (![toolbarButtons containsObject:self.myButton]) {
        // The following line adds the object to the end of the array.  
        // If you want to add the button somewhere else, use the `insertObject:atIndex:` 
        // method instead of the `addObject` method.
        [toolbarButtons addObject:self.myButton];
        [self setToolbarItems:toolbarButtons animated:YES];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top