Mike Mellor
Mike Mellor

Reputation: 1346

UIBarButtonItem, set exclusive touch

Is there a way to make UIBarButtonItem exclusive touch? At the moment you can select multiple at the same time and it keeps crashing my application.

Upvotes: 7

Views: 7478

Answers (6)

inexcii
inexcii

Reputation: 1631

for me, I used a UIButton, setting its isExclusiveTouch to true, to initialize the UIBarButtonItem.

let button = UIButton(type: .system)
button.setTitle("foo", for: .normal)
button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
button.isExclusiveTouch = true

let barButtonItem = UIBarButtonItem(customView: button)

Upvotes: 0

William George
William George

Reputation: 6795

Dredging up the past I apologise. I stumbled into this and hoped there was a better way than looping through subviews.

I found that the following makes the UIBarButtonItems exclusive:

[self.navigationController.navigationBar setExclusiveTouch:YES]; 

iOS7 may have made exclusive touch inherited.

Upvotes: 3

fjtrujy
fjtrujy

Reputation: 129

In iOS 7 it wasn't working. I have used this method to try fix it.

for(UIView *temp in self.navigationController.navigationBar.subviews){
    [temp setExclusiveTouch:YES];
    for(UIView *temp2 in temp.subviews){
        [temp2 setExclusiveTouch:YES];
    }
 }

Upvotes: 0

Shilpi
Shilpi

Reputation: 508

This does not work for UIBarButtonItem created using initWithTitle

Upvotes: -1

Mike Mellor
Mike Mellor

Reputation: 1346

Slightly easier method than subclassing the navbar but the same idea;

for(UIView *temp in self.navigationController.navigationBar.subviews)
{
    [temp setExclusiveTouch:YES];
}

Put this just after you add your bar button items.

Upvotes: 9

kengura
kengura

Reputation: 121

I managed this problem by subclassing UINavigationBar and overriding layoutSubviews method. Something like this:

- (void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *view in self.subviews) {
        view.exclusiveTouch = YES;
    }
}

Upvotes: 8

Related Questions