programmatically highlight UIBarButtonItem

2019-01-14 16:48发布

after tapping the 'record' BarButtonItem I would like to keep it programmatically highlighted until the recording is over. The highlighting graphics of iOS are very good, therefor I would like to remain or set that state.

Up to now I found 'setSelected' and 'setHighlighted' but these do not work on a UIBarButtonItem. Any suggestions on how to solve this? Thank you in advance, Koen.

9条回答
虎瘦雄心在
2楼-- · 2019-01-14 16:48

p.a.'s answer, converted for Xcode 9, Swift 4.
The idea is that the .done style highlights - or bolds, in Swift 4 - the button text.

Initialize the button item in an un-highlighted state:

let toggleButtonItem = UIBarButtonItem(title: "MyButton",
                                       style: .plain,
                                       target: self,
                                       action: #selector(doSomething))

Toggle the button item to a highlighted state using a ternary operator, like so:

toggleButtonItem.style = (toggleButtonItem.style == .plain) ?
                         toggleButtonItem.style = .done : toggleButtonItem.style = .plain

Or, alternatively, toggle the highlight state with a regular if/else statement like this instead:

if toggleButtonItem.style == .plain {
    toggleButtonItem.style = .done
}
else {
    toggleButtonItem.style = .plain
}

And, to set up a boolean value to check if the button item is highlighted:

var isHighlighted: Bool = (toggleButtonItem.style == .done)

Notes:

  • The bordered style was deprecated in iOS 8, so I used .plain here instead. They both present the button item's text in an unhighlighted state.
  • The #selector function must either be an @IBAction, or it must be prefixed with @objc, to avoid "Objective-C inference" issues. For example:

    @objc func doSomething() { ... }
    

    or, if you've connected an action to the button item:

    @IBAction func doSomething() { ... }
    

    Both of these function declarations tell the compiler that they're using Objective-C-based functionality. This is required because #selector is an Objective-C thing under the hood, and in Swift 4 you have to state this, rather than letting the compiler infer what's going on as it has done previously.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-14 16:53

If you absolutely want to use the default graphics, you could initialize your button item as

UIBarButtonItem *toggleButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"MyButton" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:someObject 
                                                                    action:@selector(doSomething:)];

and toggle it with

toggleButtonItem.style = (toggleButtonItem.style == UIBarButtonItemStyleBordered) 
                         ? UIBarButtonItemStyleDone : UIBarButtonItemStyleBordered;

You would also need to use the style property to read the current state.

BOOL isSelected = (toggleButtonItem.style == UIBarButtonItemStyleDone)
查看更多
相关推荐>>
4楼-- · 2019-01-14 16:54

You create an outlet of this button for example btnMoreOut and you do:

btnMoreOut.tintColor = [UIColor colorWithRed:0.882 green:0.722 blue:0.169 alpha:1];

I hope this helps..Good luck :)

查看更多
虎瘦雄心在
5楼-- · 2019-01-14 16:57

setSelected and setHighlighted work fine on UIControls, but not UIBarButtonItems (which are not UIControls).

I'd recommend using UIBarButtonItem's - (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics (documentation linked) method to change the background image to something that mimics highlighting.

You can also set a custom UIView on the item which also mimics highlighting (see the customView property).

查看更多
小情绪 Triste *
6楼-- · 2019-01-14 17:03

You can try this (Swift):

    func setupInterface(){

    var myButton = UIBarButtonItem()
    if (your_condition){
      myButton = UIBarButtonItem(image: UIImage(named: "img_selected"), style: .Plain, target: self, action: Selector("DoSomething:"))
    }
    else{
        myButton = UIBarButtonItem(image: UIImage(named: "img_unselected"), style: .Plain, target: self, action: Selector("DoSomething:"))
    }
    navigationItem.rightBarButtonItem = myButton
  }

Call setupInterface() in ViewDidLoad and another function in your code.

查看更多
小情绪 Triste *
7楼-- · 2019-01-14 17:08

If you add a UIBarButtonItem with a UIButton backing it, you can just ask for the CustomView.

UIBarButtonItem with a backing UIButton

UIButton *button = (UIButton *)[self.barButtonItem customView];
[button setSelected:YES];
查看更多
登录 后发表回答