6

プログラムで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;

警告: 「UIBarButtonItem *__strong」から「UIView *」に割り当てられている互換性のないポインター型

4

10 に答える 10

14

このコードを使用してみて、うまくいくかどうかを確認してください

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];
于 2012-11-12T10:02:33.613 に答える
10

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;
于 2012-11-12T10:01:24.707 に答える
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
}

ここに画像の説明を入力

于 2016-05-12T22:14:28.423 に答える
2

これは、@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
于 2018-10-18T05:52:05.013 に答える
1

スウィフト 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
于 2017-06-16T13:03:15.023 に答える
0

ビューにボタンを追加し、そのビューをナビゲーション バーのタイトル ビューとして作成すると、実行時にボタンのタイトルを簡単に変更できます。

于 2012-11-12T10:01:46.313 に答える
0

UIBarButtonItem の代わりに、ボタンの UIButton オブジェクトを作成してみてください。UIButton は UIView のサブクラスであるため、作成した UIButton に titleView プロパティを設定できるはずです。

于 2012-11-12T10:00:49.747 に答える