繁体   English   中英

如何设置navigationItem titleView的按钮?

[英]How to set button for navigationItem titleView?

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

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

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

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

self.navigationItem.titleView = navigationTitleButton;

Warnng说:从'UIBarButtonItem * __ strong'分配给'UIView *'的指针类型不兼容

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

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

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

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;

要么

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

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

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

按钮的动作方法如下:

func clickOnButton(button: UIButton) {

}

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

在这个例子中,我还绕过角落并添加了知道它是否被轻敲的能力。

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
}

在此输入图像描述

这是@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版本:

    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

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

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

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

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM