UIBarButtonItem, set exclusive touch
Asked Answered
M

6

7

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.

Meganmeganthropus answered 10/7, 2012 at 9:30 Comment(0)
M
9

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.

Meganmeganthropus answered 5/4, 2013 at 8:23 Comment(0)
P
8

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;
    }
}
Parotid answered 10/1, 2013 at 8:41 Comment(0)
R
3

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.

Riddick answered 13/5, 2014 at 13:40 Comment(1)
not working in iOS15Zackzackariah
S
0

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];
    }
 }
Studdard answered 26/2, 2014 at 12:4 Comment(0)
Z
0

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)
Zackzackariah answered 9/3, 2022 at 7:49 Comment(0)
S
-1

This does not work for UIBarButtonItem created using initWithTitle

Servitude answered 9/12, 2013 at 15:38 Comment(1)
what doesn't work? shouldn't this be a comment on someone else's post?Meganmeganthropus

© 2022 - 2024 — McMap. All rights reserved.