![](/img/trans.png)
[英]SwiftUI Show navigation bar title on the back button but not in the previous View
[英]navigation title on previous screen disappear when back button pressed
我有導航欄標題的問題。 我有 2 個屏幕,當我單擊屏幕 2 上的后退按鈕時,它將返回到屏幕 1,但屏幕 1 的標題消失了。 這是我的代碼片段
屏幕1.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"title 1";
....
屏幕 2.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = self.stringTitle;
// change the back button and add an event handler
self.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:@"btn_back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(-12, 2, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width-15, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
self.navigationItem.leftBarButtonItem = customBarItem;
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
....
}
- (void) backAction
{
[self.navigationController popViewControllerAnimated:YES];
}
我嘗試了幾種技巧來使用以下技巧使屏幕 1 的標題出現:
在viewwillappear
方法上設置標題
- (void)viewWillAppear:(BOOL)animated{self.title = @"tes";}
在viewdidappear
方法上設置標題
- (void)viewDidAppear:(BOOL)animated{ self.navigationController.navigationBar.topItem.title = @"YourTitle"; self.title = @"tes"; }
但標題仍然消失了。 請幫忙
這是我移動到下一個屏幕的方式
- (IBAction)btGenerateTokenAction:(id)sender {
SofttokenResultController *controller = [[SofttokenResultController alloc] initWithNibName:@"SofttokenResultController" bundle:nil];
controller.navigationController.navigationBar.backItem.title = @"Kembali";
[ViewLoadingUtil animateToNextView:controller from:self :@""];
}
+ (void) animateToNextView:(UIViewController *)controller from:(UIViewController *)fromController :(NSString *)navTitle{
NSLog(@"animateToNextView called");
[fromController.navigationController pushViewController:controller animated:YES];
}
這解決了我的問題:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated: animated)
self.navigationItem.title="Title text"
}
就我而言,問題是backItem?.title
。 在我的視圖控制器和UINavigationController
的自定義子類中,我將其設置為空字符串:
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
...
navigationController.navigationBar.backItem?.title = ""
}
我的猜測是,當使用popViewController(animated:)
,標題取自backItem?.title
並設置為主標題屬性。
在viewWillAppear
實現:
self.navigationItem.title="Title text";
它通常在您設置 backItem?.title = "" 時發生,因為后部控制器的標題取自此屬性我幾個月來就遇到了這個問題,僅通過評論這一行就解決了
Objective c
解決方案:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear: animated];
self.navigationItem.title = @"Title text";
}
為我工作解決方案。 你可以試試這個:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.topItem?.title = ""
self.navigationItem.title = "Title"
}
我來自 Swift 世界。 但是我嘗試了很多解決方案。 這些都是由動畫引起的。 如果您將 Bool 更改為 false,它將解決您的問題。 最后,您可以將代碼放入 viewWillAppear 函數中。
override func viewWillAppear(animated: Bool) {
if animated {
//...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.