模态视图控制器 - UIBarButtonItems不持久(Modal View Controll

2019-10-29 17:19发布

我介绍模态视图控制器,封装在一个UINavigationController。

当他们第一次出现,该栏按钮的项目是好的。

但是,如果我点击一个按钮,一个新的视图控制器推到UINavigationController的,然后我用视图控制器的后退按钮,它被赶回到原来的模态视图控制器 的按钮显示不出来 ,(但他们的工作,当我点击他们 - >就是不能看到它们)

我似乎无法找出为什么发生这种情况。

下面是一些代码。

哦,对了,我自定义导航器的导航栏的背景。 我有一种感觉,这可能会造成这种视觉干扰,但我不知道是什么改变来获得它的权利。

在GGMainViewController:

GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];

- (void)setupModalNavigationBar:(UINavigationController *)nav {
    // unnecessary code removed for a quicker read
    // basically navigation bar has a background gradient as well as a bottom border
    [nav.navigationBar.layer addSublayer:bottomBorder];
    [nav.navigationBar.layer addSublayer:gradient];

}

在GGSelectTeamsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    title = [[UILabel alloc] init];
    title.text = someText;
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    [title sizeToFit];

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    backButton.frame = someFrame;
    [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];


    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectButton setTitle:@"Select" forState:UIControlStateNormal];
    [selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    selectButton.frame = someFrame;
    [selectButton addTarget:self action:@selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}

由于导航项目在viewDidLoad方法设置,为什么我不能看到他们(但他们仍然是功能性的),当我从另一个视图控制器恢复呢?

Answer 1:

这似乎是这样做的过于复杂的方式。

你为什么不创建自己的按钮,然后使用setRightBarButtonItems:项目:方法从UINavigationBar的设置你想要的按钮?

我猜他们消失了,因为你将它们添加到UINavigationBar的层。

不过,如果你想保持这种方法与层,你可能需要添加他们在viewWillAppear中的方法,但您可能需要调用removeFromSuperlayer,以确保你不添加更多的东西超过一次。

希望这可以帮助。



Answer 2:

问题是:

  • 梯度为导航栏(由代码完成)

解决的办法是:

  • 使用草图来创建梯度的图像,并且使用该外观代理向图像设置为背景图像

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];



文章来源: Modal View Controllers - UIBarButtonItems Do Not Persist