IOS - UIBarButtonItem identifier - the ability to create a "settings" button cogwheel

I want to create a UIBarButtonItem to represent application settings (cogwheel). Currently I can find an option to create a UIBarButtonItem (Interface Builder> Attributes Inspector> identifier), such as Add (+), Modify, Finish, Cancel, etc.

I can not find the option to create a settings icon (cogwheel). Is there a way to do this in the interface builder or through code?

Or do I need to create an image and then an image?

+45
ios xcode interface-builder uibarbuttonitem
Mar 18
source share
4 answers

There are some noteworthy examples in Unicode that you can simply copy and paste into a declaration string in Xcode or use the standard Unicode String Escape (\ uxxxx), and iOS is actually pretty fluent about Unicode (I know some of the char are pretty ugly but Unicode for ya '):

Unicode Character "GEAR WITHOUT HUB" (U + 26ED): http://www.fileformat.info/info/unicode/char/26ed/index.htm

Unicode Character 'GEAR' (U + 2699): http://www.fileformat.info/info/unicode/char/2699/index.htm

Or prepare an image and adjust the customView UIBarButtonItem property accordingly.

+62
Mar 18 '12 at 3:14
source share

Composing the answers of CodaFi and user1046037 :

Create a UIBarButtonItem with a Unicode character as the title.

You should initialize the UIBarButtonItem with the header ( initWithTitle: , not the system element ( initWithBarButtonSystemItem: .

You can set your own header with a string (for example, a unicode character).

You can resize the title.

 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
Feb 20 '14 at 8:11
source share

To get the cogwheel icon using Swift, follow these steps in viewDidLoad() , assuming you have connected your button to your controller. (Example: @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) } 
+5
on April 26 '15 at 7:37
source share

This works for me in Swift 3 and 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) 

See @hazernut comment in the accepted answer. Without adding \u{0000FE0E} to the line, it is displayed as emoji and is immune to any appearance settings. Adding this line fixes this.

+3
May 16 '17 at 12:04
source share



All Articles