找到你要的答案

Q:iOS: remove text from Back button keeping the icon

Q:iOS:删除文本从后退按钮保持图标

I want to remove the text from the back button, but I want to keep the icon. I have tried

let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
        navigationItem.leftBarButtonItem = backButton

However, this removes completely the text and the icon. I am using Swift 2.0.

Thanks in advance.

我想从后面的按钮删除文本,但我想保留图标。我已经试过了

let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
        navigationItem.leftBarButtonItem = backButton

然而,这完全删除的文字和图标。我用雨燕2。

先谢谢了。

answer1: 回答1:

Method of @rmd2 is almost right =) But instead you should select navigation bar of the controller to which back button will point to and type " " in the Back Button field.

“rmd2方法几乎是正确的=)而是你应该选择导航栏控制器,后退按钮将指向型”“后退按钮字段。

answer2: 回答2:

I know this already has an answer, but you can also do it in code (in case you're working with nibs)

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

Add the above in the first view controller.

Note that you need to do this for each view controller that is pushing. So if you have 3 view controllers, and you want to remove the back text from all of them, you'll have to add the line in view controller 1 and 2.

我知道这已经有答案,但你也可以在代码中做的(如果你的工作尖)

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)

在第一个视图控制器中添加上述。

注意,你需要为每一个视图控制器做这是推。因此,如果你有3个视图控制器,并且你想删除所有的后面的文本,你必须在视图控制器1和视图中添加行。

answer3: 回答3:

I solved this problem by adding a " " on the StoryBoard Title of the previous ViewController. Just a space, not empty ;D

我加入了“在前视图的故事标题解决了这个问题。只是一个空间,而不是空虚

answer4: 回答4:

If you wanted to remove the title of back button '< Settings' to '<' in subSettingsViewController then you've to set backBarButtonItem title in SettingsViewController's viewWillDisappear() method.

Objective-C:

- (void)viewWillDisappear:(BOOL)animated
    [super viewWillDisappear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
}

Swift:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}

如果你想消除背部按钮& lt称号;设置“<;”subsettingsviewcontroller然后你在settingsviewcontroller的viewwilldisappear()方法集backBarButtonItem标题。

Objective-C:

- (void)viewWillDisappear:(BOOL)animated
    [super viewWillDisappear:animated];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
}

Swift:

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}
answer5: 回答5:
let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)

self.navigationItem.leftBarButtonItem = barButton

func backButtonPressed(btn : UIButton) {

    // Your code
}
let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)

self.navigationItem.leftBarButtonItem = barButton

func backButtonPressed(btn : UIButton) {

    // Your code
}
ios  swift  swift2  xcode7