iPhone导航栏不会显示设备上的所有按钮

时间:2013-11-04 15:43:41

标签: ios iphone objective-c navigationbar

在iPhone模拟器上的纵向模式或设备上的横向模式下,它会显示所有导航栏按钮,但在设备上以纵向模式使用时,不会显示该按钮。以下是导航栏的图像。

Simulator shows button

Device doesn't show button

我测试的设备是运行iOS 6.1.3(10B329)的iPhone 4S。我使用的模拟器是运行iOS 6.0 / 6.1的7.0版(463.9.4)。

我正在考虑在编辑模式下删除搜索按钮,但无论模式如何,我都希望将此选项保留给用户。

感谢任何帮助或见解,谢谢。

修改 最初创建右侧按钮并在viewDidLoad:中为ViewController添加,如下所示:

_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];

进入编辑模式时:

- (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];
    }
}

2 个答案:

答案 0 :(得分:1)

老实说,那很奇怪。也许标题的某些属性使其占据主导地位。根据我的经验,在“编辑模式”期间最好选择较少的选项。如果你决定走这条路,这里有一些可能有用的代码。 (当然,您的变量名称很可能不同)

// 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];
    }
}

答案 1 :(得分:0)

我选择了“较少选项”选项,专门用于纵向模式的iPhone,因为我找不到正确的消失按钮解决方案,而且确实没有足够的空间。在横向和iPad上,第三个按钮_searchBarButtonItem有空间显示,因此会显示。

以下是我为获得所需行为所做的更改。我希望有些人能发现这很有用。

-(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];
        }
    }
}