简体   繁体   English

如何设置navigationItem titleView的按钮?

[英]How to set button for navigationItem titleView?

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' Warnng说:从'UIBarButtonItem * __ strong'分配给'UIView *'的指针类型不兼容

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

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;

Swift version: Swift版本:

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) {

}

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. 您可以直接添加UIImageView作为navigationItem的titleView,但如果您不确定它的大小,它将不可见。 The easiest way to size it is to use sizeToFit(). 调整大小的最简单方法是使用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
}

在此输入图像描述

Here is a Swift 4 ported 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

Swift 3 Version: Swift 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

If you plan on changing the button.title at runtime, and you change the title to longer text, it will not be centered. 如果您计划在运行时更改button.title,并将标题更改为更长的文本,则它将不会居中。 And if you do button.sizeToFit(), it will be centered, but show the new title as "T..e". 如果你执行button.sizeToFit(),它将居中,但将新标题显示为“T..e”。

The solution is to put the UIButton in a UIView. 解决方案是将UIButton放在UIView中。 And constrain the button to all sides of UIView. 并将按钮限制在UIView的所有方面。 Then just set the navigationItem.titleView to that UIView. 然后将navigationItem.titleView设置为该UIView。 And it will handle programmatic title changes perfectly. 它将完美地处理程序化的标题更改。

Try creating a UIButton object for your button instead of a UIBarButtonItem one. 尝试为您的按钮而不是UIBarButtonItem创建一个UIButton对象。 UIButton is a subclass of UIView so you should be able to set the titleView property to the UIButton you've created. UIButton是UIView的子类,因此您应该能够将titleView属性设置为您创建的UIButton。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为所有navigationcontroller和navigationitem设置titleview - Set titleview for all navigationcontroller and navigationitem 如何设置navigationItem.titleView图像水平居中? - How to set navigationItem.titleView image horizontal center? 在某些情况下如何设置具有标题视图的导航项目的标题 - How to set title of navigationitem which have titleview for some conditions iOS 11 navigationItem.titleView 宽度未设置 - iOS 11 navigationItem.titleView Width Not Set 如何在屏幕之间保留navigationItem.titleView? - How to persist navigationItem.titleView between screen? 我如何将navigationItem.titleView放在左右栏按钮项的后面 - how do I have the navigationItem.titleView to be behind left and right bar button item 如何将UISearchController的searchBar设置为不是tableHeaderView或navigationItem.titleview的视图? - How do I set the UISearchController's searchBar to a view that isn't the tableHeaderView or navigationItem.titleview? 在navigationItem中居中titleView - centering titleView in navigationItem 如何在NavigationItem titleView中将自定义视图居中 - How to center custom view from xib in navigationItem titleView 如何让 iOS navigationItem.titleView 左对齐? - How to make iOS navigationItem.titleView align to the left?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM