如何设置navigationItem titleView的按钮?

时间:2012-11-12 09:56:29

标签: ios xcode uiview uibutton uinavigationitem

我想以编程方式为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 *'的指针类型不兼容

9 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分:10)

您不必创建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;

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

答案 2 :(得分:2)

您可以直接添加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
}

enter image description here

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

这是@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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

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