0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カスタムボタンを作る

0
Posted at

画像を使ったカスタムボタンを作成してみたので、手順を残したいと思います。

ContactsTableViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
         
    let inviteButton =  UIBarButtonItem(image: UIImage(named: "invite"), style: .plain, target: self, action: #selector(self.inviteButtonPressed))
            
    let searchButton = UIBarButtonItem(image: UIImage(named: "nearMe"), style: .plain, target: self, action: #selector(self.searchNearByButtonPressed))
            
    self.navigationItem.rightBarButtonItems = [inviteButton, searchButton]
}

UIBarButtonItemクラスを作成し、UIImage(named: "invite")でボタンの画像を指定しています。
画像の名前を指定して下さい。
#selectorでタップしたときの処理を記載しています。

navigationItem.rightBarButtonItems = [inviteButton, searchButton]でどこに配置するかを定義しています。
今回はnavigationItem.rightBarButtonItemsなので、右上の方に表示させています。
さらに、[inviteButton, searchButton]でinviteButtonから右に配置しています。

見て把握できるのがいいですね。
配列のボタンは画面の右から配置されることだけ注意が必要です。

参照

【iPhoneアプリ開発】UINavigationBarに複数のUIBarButtonItemを配置する方法

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?