Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - UIBarButtonItem Identifier - option to create "settings" cogwheel button

I want to create a UIBarButtonItem to represent the app's settings (cogwheel). Presently I can only find an option to create UIBarButtonItem (Interface Builder > Attributes Inspector > identifier) such as "Add" (+), "Edit", "Done", "Cancel" etc

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

Or do I have to create an image and then the image ?

like image 851
user1046037 Avatar asked Mar 18 '12 00:03

user1046037


4 Answers

Unicode has several notable examples you can simply copy and paste into a string declaration in Xcode, or use the standard Unicode String Escape (\uxxxx) and iOS is actually quite fluent when it comes to Unicode (I know some of the char's are fairly ugly, but that's 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 set the UIBarButtonItem's customView property accordingly.

like image 70
CodaFi Avatar answered Oct 11 '22 22:10

CodaFi


Composing CodaFi and user1046037 answers:

Creating UIBarButtonItem with unicode character as a title.

You have to initialize UIBarButtonItem with title (initWithTitle:) not system item (initWithBarButtonSystemItem:).

You can set custom title with string (such as unicode character).

You can resize 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];
like image 26
GxocT Avatar answered Oct 12 '22 00:10

GxocT


This works in Swift 5 and iOS 14.4...

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

See @hazelnut's comment in the accepted answer. Without adding \u{0000FE0E} to the string it shows up as an emoji and is immune to any appearance settings. Adding that string fixes that.

like image 10
Murray Sagal Avatar answered Oct 11 '22 23:10

Murray Sagal


To get the cogwheel icon using Swift, do the following in viewDidLoad(), assuming you have wired up your button from your view into your controller. (Ex: @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)
}
like image 6
Ε Г И І И О Avatar answered Oct 11 '22 23:10

Ε Г И І И О