Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use info button on a UIBarButtonItem

How can I use the info light on a UIBarButtonItem?

I don't want to use a custom image because the result is not very nice.

I would like to use the Apple "info" button on a UIBarButtonItem using Swift.

like image 645
DanielZanchi Avatar asked Jan 10 '16 21:01

DanielZanchi


People also ask

How do I change my UIBarButtonItem text?

Select the UIBarButtonItem in Interface Builder , open the inspector (Command + Shift + I) , and select "Custom" under the dropdown next to Identifier .

How do I change the size of my UIBarButtonItem?

You can set the font size for a specific BarButtonItem by overriding its ContentTemplate. Put an AccessText to this template and set its FontSize property. If you want to change the font size for all items in a specific BarManager, use the attached TextBlock. FontSize property.

What is UIBarButtonItem Swift?

A specialized button for placement on a toolbar, navigation bar, or shortcuts bar. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

There is no direct way to create such a bar button using the UIBarButtonItem APIs.

You can although use a custom view configured with a .InfoLight UIButton, as suggested here:

// Create the info button
let infoButton = UIButton(type: .infoLight)

// You will need to configure the target action for the button itself, not the bar button itemr
infoButton.addTarget(self, action: #selector(getInfoAction), for: .touchUpInside)

// Create a bar button item using the info button as its custom view
let infoBarButtonItem = UIBarButtonItem(customView: infoButton)

// Use it as required
navigationItem.rightBarButtonItem = infoBarButtonItem

Feel free to leave a comment if you need any more help.

like image 83
mokagio Avatar answered Oct 01 '22 02:10

mokagio


Adapting mokagio's excellent answer to Obj-C:

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(getInfoAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = infoBarButtonItem;
like image 35
T'Pol Avatar answered Oct 01 '22 01:10

T'Pol