web-dev-qa-db-ja.com

iOS-UIBarButtonItem Identifier-「設定」歯車ボタンを作成するオプション

アプリの設定(歯車)を表すUIBarButtonItemを作成します。現在、「追加」(+)、「編集」、「完了」、「キャンセル」などのUIBarButtonItem(インターフェイスビルダー>属性インスペクター>識別子)を作成するオプションのみを見つけることができます。

設定(歯車)アイコンを作成するオプションが見つかりません。インターフェイスビルダーまたはコードを使用してこれを行う方法はありますか?

または、イメージを作成してからイメージを作成する必要がありますか?

45
user1046037

Unicodeにはいくつかの注目すべき例があり、Xcodeの文字列宣言に単純にコピーして貼り付けるか、標準のUnicode文字列エスケープ(\ uxxxx)を使用します。 、それはyaのUnicodeです '):

Unicode文字「GEAR WITHOUT HUB」(U + 26ED): http://www.fileformat.info/info/unicode/char/26ed/index.htm

Unicode文字「GEAR」(U + 2699): http://www.fileformat.info/info/unicode/char/2699/index.htm

または、画像を準備し、それに応じてUIBarButtonItemのcustomViewプロパティを設定します。

63
CodaFi

CodaFiおよびser1046037回答の作成:

Unicode文字をタイトルとして使用してUIBarButtonItemを作成します。

UIBarButtonItemをタイトル(initWithTitle:)システム項目ではありません(initWithBarButtonSystemItem:)。

文字列(Unicode文字など)でカスタムタイトルを設定できます。

タイトルのサイズを変更できます。

UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc] initWithTitle:@"\u2699" style:UIBarButtonItemStylePlain target:self action:@selector(showSettings)];

UIFont *customFont = [UIFont fontWithName:@"Helvetica" size:24.0];
NSDictionary *fontDictionary = @{NSFontAttributeName : customFont};
[settingsButton setTitleTextAttributes:fontDictionary forState:UIControlStateNormal];
15
GxocT

Swiftを使用して歯車アイコンを取得するには、ビューからコントローラーにボタンを接続したと仮定して、viewDidLoad()で次の操作を行います。 (例:@IBOutlet var settingsButton: UIBarButtonItem!

    self.settingsButton.title = NSString(string: "\u{2699}") as String
    if let font = UIFont(name: "Helvetica", size: 18.0) {
        self.settingsButton.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)
    }
6

これはSwift 3およびiOS 10で動作します...

let settingsButton = UIBarButtonItem(title: NSString(string: "\u{2699}\u{0000FE0E}") as String, style: .plain, target: self, action: #selector(settings))
let font = UIFont.systemFont(ofSize: 28) // adjust the size as required
let attributes = [NSFontAttributeName : font]
settingsButton.setTitleTextAttributes(attributes, for: .normal)

受け入れられた回答の@hazernutのコメントを参照してください。 \u{0000FE0E}を文字列に追加しないと、絵文字として表示され、外観設定の影響を受けません。その文字列を追加すると修正されます。

6
Murray Sagal