xcode 如何为导航项 titleView 设置按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13341562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:04:05  来源:igfitidea点击:

How to set button for navigationItem titleView?

iosxcodeuiviewuibuttonuinavigationitem

提问by iWizard

I want to programmatically set button for navigationItem.titleView.

我想以编程方式为 navigationItem.titleView 设置按钮。

I tried with following code but with no success;

我尝试使用以下代码但没有成功;

UIBarButtonItem *navigationTitleButton = [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStyleBordered target:self action:@selector(backToHOmePage)];

navigationTitleButton.image = [UIImage imageNamed:@"title.png"];

self.navigationItem.titleView = navigationTitleButton;

Warnng says: Incompatible pointer types assigning to 'UIView *' from 'UIBarButtonItem *__strong'

警告说:不兼容的指针类型从“UIBarButtonItem *__strong”分配给“UIView *”

回答by Janub

Try to use this code see if it works for you

尝试使用此代码,看看它是否适合您

UIView * container = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:@selector(backToHOmePage) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:@"clanak_default.png"] forState:UIControlStateNormal];
[button sizeToFit];
[container addSubview:button];
[button release];
[container sizeToFit];
self.navigationItem.titleView = container;
[container release];

回答by Evgeniy S

You dont have to create BarItem, you need to create UIView class object;

你不必创建 BarItem,你需要创建 UIView 类对象;

ARC

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
view.backgroundColor = [UIColor clearColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = view.frame;

[view addSubview:button];

self.navigationItem.titleView = view;

or

或者

   UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   button.frame = CGRectMake(0, 0, 40, 40);
   self.navigationItem.titleView = button;

回答by Esqarrouth

Swift version:

迅捷版:

var button =  UIButton.buttonWithType(UIButtonType.Custom) as UIButton
button.frame = CGRectMake(0, 0, 100, 40) as CGRect
button.backgroundColor = UIColor.redColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("clickOnButton:"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.titleView = button

Here you can see in last line button is directly set to the titleView of navigationItem which will add button at the center of navigation bar.

在这里你可以看到最后一行按钮直接设置为navigationItem的titleView,它将在导航栏的中心添加按钮。

Action method for button is below:

按钮的动作方法如下:

func clickOnButton(button: UIButton) {

}

回答by Michael Peterson

You can add a UIImageView as the navigationItem's titleView directly, however it will not be visible if you do not make sure it is sized. The easiest way to size it is to use sizeToFit().

您可以直接添加一个 UIImageView 作为导航项的 titleView,但是如果您不确保它的大小,它将不可见。调整大小的最简单方法是使用 sizeToFit()。

In this example I also round the corners and add the ability to know if it is tapped.

在这个例子中,我还圆角并添加了知道它是否被点击的能力。

override func viewDidLoad() {
    super.viewDidLoad()

    let titleIconButton = UIButton(type: .Custom)
    titleIconButton.setImage(UIImage(named: "login-icon"), forState: .Normal)
    titleIconButton.addTarget(self, action: #selector(titleIconTap), forControlEvents: .TouchUpInside)
    titleIconButton.clipsToBounds = true
    titleIconButton.layer.cornerRadius = 4
    titleIconButton.sizeToFit()
    navigationItem.titleView = titleIconButton
}

enter image description here

在此处输入图片说明

回答by Zahnon

Here is a Swift 4ported version of the answer from @Esqarrouth.

这是@Esqarrouth 答案的Swift 4移植版本。

let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = .red
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(clickedButton(_:)), for: .touchUpInside)
self.navigationItem.titleView = button

回答by Sébastien REMY

Swift 3 Version:

斯威夫特 3 版本:

    let logoButton = UIButton(type: .custom)
    logoButton.setImage(#imageLiteral(resourceName: "Logo"), for: .normal)
    logoButton.sizeToFit()
    logoButton.addTarget(self, action: #selector(tapLogo), for: .touchUpInside)

    self.navigationItem.title = ""
    self.navigationItem.titleView = logoButton

回答by Harris

If you plan on changing the button.title at runtime, and you change the title to longer text, it will not be centered. And if you do button.sizeToFit(), it will be centered, but show the new title as "T..e".

如果您计划在运行时更改 button.title,并将标题更改为更长的文本,则它不会居中。如果您执行 button.sizeToFit(),它将居中,但将新标题显示为“T..e”。

The solution is to put the UIButton in a UIView. And constrain the button to all sides of UIView. Then just set the navigationItem.titleView to that UIView. And it will handle programmatic title changes perfectly.

解决方案是将 UIButton 放在 UIView 中。并将按钮约束到 UIView 的所有侧面。然后只需将 navigationItem.titleView 设置为该 UIView。它将完美地处理程序化的标题更改。

回答by NikosM

Try creating a UIButton object for your button instead of a UIBarButtonItem one. UIButton is a subclass of UIView so you should be able to set the titleView property to the UIButton you've created.

尝试为您的按钮创建一个 UIButton 对象而不是 UIBarButtonItem 对象。UIButton 是 UIView 的子类,因此您应该能够将 titleView 属性设置为您创建的 UIButton。

回答by spider1983

You can add your button on a view then make that view as the title view of navigation bar then you can change the title of the button at run time easily.

您可以在视图上添加按钮,然后将该视图作为导航栏的标题视图,然后您可以在运行时轻松更改按钮的标题。

回答by Johannes Fahrenkrug

This UISegmentedControl-based approach might help you: https://stackoverflow.com/a/16792575/171933Enjoy!

这种基于 UISegmentedControl 的方法可能对您有所帮助:https: //stackoverflow.com/a/16792575/171933享受吧!