Help us understand the problem. What is going on with this article?

<Swift> 隠れUIBarButtonItemを出現させる

More than 3 years have passed since last update.

TL;DR

非公開APIを呼び出すことで、UIToolbarの下記Itemが使えるようになります(通常は利用不可)。

  • Safariの「進む」「戻る」「上」「下」ボタン
  • 地図の「現在地」ボタン

UIToolbarの画像問題

アプリ内ブラウザを作るときに、頭を悩ませるのが、UIToolbarの「進む」「戻る」ボタン画像の調達・設定です。

ネットには使えそうな画像がたくさん転がっているものの、実際に当ててみると微妙にサイズが違ったり、線の太さが意図したものと違ったりで苦労します。

そこで、コードですべてをまかないたくなりますが、UIBarButtonItemでは、「進む」「戻る」などの基本的なものは、UIBarButtonSystemItemとして公開されていません(代わりになぜか「ゴミ箱」などが公開されています…)。

今回は、これらを使えるようにUIBarButtonItemをいじってみます。

UIBarButtonItemを拡張

extension UIBarButtonItem {
    enum HiddenItem: Int {
        case Arrow = 100
        case Back = 101
        case Forward = 102
        case Up = 103
        case Down = 104
    }

    convenience init(barButtonHiddenItem: HiddenItem, target: AnyObject?, action: Selector?) {
        let systemItem = UIBarButtonSystemItem(rawValue: barButtonHiddenItem.rawValue)
        self.init(barButtonSystemItem: systemItem!, target: target, action: action)
    }
}

Toolbarに設定

        let items =
        [
            UIBarButtonItem(barButtonHiddenItem: .Arrow, target: nil, action: nil),
            UIBarButtonItem(barButtonHiddenItem: .Back, target: nil, action: nil),
            UIBarButtonItem(barButtonHiddenItem: .Forward, target: nil, action: nil),
            UIBarButtonItem(barButtonHiddenItem: .Up, target: nil, action: nil),
            UIBarButtonItem(barButtonHiddenItem: .Down, target: nil, action: nil)
        ]

        self.navigationController?.setToolbarHidden(false, animated: false)
        self.setToolbarItems(items, animated: false)

標準ボタンの出現!

あとは、可変、固定スペースを入れて調整してあげればいいだけですね。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした