簡體   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