I know some have already asked the question but so far, the answers are not really specific.
Apparently apple have made the click area of the NavigationBar larger than it really is, but I don't think it's supposed to be that large.
In my app, there is a TableView right underneath the NavBar and you can click all the way down to half of the first cell to trigger the event of the rightBarButtonItem.
the button is instanced like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
[[self navigationItem] setRightBarButtonItem:editButton];
"self" is the root ViewController of a NavigationController.
As you could imagine, it's a problem since the cells are selectable to push another ViewController.
I managed to go around the problem by making the cells' height bigger but I'd rather have them at the regular size.
I'm sure I'm not the only one with this case of scenario.
Thanks in advance.
UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButton addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[menuButton setImage:[UIImage imageNamed:#"menuIcon"] forState:UIControlStateNormal];
UIView *menuButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[menuButtonContainer addSubview:menuButton];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButtonContainer];
This perfectly worked for me....
You can't do much about the touch area. It's all based on how much finger area is picked up by the display. You can calculate size of touch area (warning: undocumented api) but intercepting touch events and calling a method to correct for fat finger syndrome seems a bit over-the-top.
Basic process is:
You touch screen
Change of capacitance is measured in a unique, individualized portion of the screen and sent to hardware/software to interpret (just as an aside, Apple's patent states as many as 15 touches can be tracked at once.. 10 fingers, two palms, and 3 others in case you ever make an iPhone "Twister" game)
Software interprets gradients in capacitance on screen and identifies touch boundaries
Software calculates centroid of touch based on the average capacitance magnitude for each pixel or point in the region
Software sends coordinates of touch boundary locations into application
Your app interprets as a touch event, it gets sent along the responder chain, and (hopefully) the user see's the touch on the screen
The other answer here from iosDeveloper about putting a container view around the button can help reduce the size of the clickable area on the left and right of the button, but doesn't help with the issue I had where you would activate the button clicking below the navigation bar.
After hours of searching several different SO threads and trying various things, I finally found my answer to this issue here: https://stackoverflow.com/a/10597699/1289240
Since i use a transparent toolbar and tint the UIBarButtonItem, I couldn't use a custom view as I would need to set tint, width/height, rounded corners to make it similar to UIBarButtonItem look
I therefore use a invisible button which I add to my items to resolve this issue. and events are trappped there
in #interface UIBarButtonItem (ImageButton)
+ (UIBarButtonItem*) invisibleButtonForTransparentBar {
static UIBarButtonItem* fakeBtn = nil;
if (!fakeBtn) {
fakeBtn = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] init]];
}
return fakeBtn;
}
in View Controller:
[self.viewToolbar setItems: #[flexSpace,
[UIBarButtonItem fakeLastButtonForTransparentBar],
self.btnTrackUser,
self.btnMapConfig,
[UIBarButtonItem fakeLastButtonForTransparentBar],
flexSpace] ];
We are unable to reduce the default uibarbuttonitem size. The idea is to make a uibutton, add to a uiview , then to uibarbuttonitem's custom view.
The size of the uiview containing uibutton will be the click area of the uibarbutton.
Here is my code:
self.navigationController?.navigationBarHidden = false
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64)) // Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.blueColor()
navigationBar.delegate = self;
// Create a navigation item with a title
let navigationItem = UINavigationItem()
//menu button
let menubutton: UIButton = UIButton(frame: CGRectMake(0, 0, 30, 30))
menubutton.setImage(UIImage(named: "menu"), forState: UIControlState.Normal)
menubutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)
//menu button custom view
let leftView = UIView(frame: CGRectMake(0,0,30,30))
leftView.addSubview(menubutton)
//left uibarbutton
let leftItem:UIBarButtonItem = UIBarButtonItem(customView: leftView)
navigationItem.leftBarButtonItem = leftItem
//searchButton
let searchbutton: UIButton = UIButton()
searchbutton.setImage(UIImage(named: "search1x"), forState: UIControlState.Normal)
searchbutton.frame = CGRectMake(0, 0, 30, 30)
searchbutton.addTarget(self, action: "btn_clicked", forControlEvents: UIControlEvents.TouchUpInside)
//menu button custom view
let rightView = UIView(frame: CGRectMake(0,0,30,30))
rightView.addSubview(searchbutton)
//right uibarbutton
let rightItem:UIBarButtonItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightItem
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
Note:
Ex:- menu button is a UIButton and added to leftView. The leftItem is a UIBarButtonItem and is added to leftBarButtonItem of the navigationBar. The leftView is added as the custom view of leftItem. So, the click area of leftBarButtonItem of the navigationBar is the frame of leftView. i.e., frame: CGRectMake(0,0,30,30).
All I can offer is to create UIBarButtonItem with custom view
let view = CustomView()
let itemButton = UIBarButtonItem(customView: view)
Then override this view's touchesEnded
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
if touches.count == 1,
let touch = touches.first {
let touchPoint = touch.location(in: self)
if bounds.contains(touchPoint) {
someAction()
}
}
}
Don't forget to make it user interaction enabled somewhere in the init isUserInteractionEnabled = true.
Related
Is it possible to adjust the title of a UIBarButtonItem inside an UIToolbar?
I've tried the following lines of code without success:
UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)
And this one:
self.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Compact)
Offset an UIBarButtonItem (Text!) inside a UIToolBar vertically seems not to work with Xcode 7 (Beta 6).
You are right, according to the docs, this should do it:
UIBarButtonItem.appearance().setTitlePositionAdjustment
either globally in AppDelegate:
UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)
or locally with the item outlet itself:
(Swift)
self.myBarItem.setTitlePositionAdjustment(UIOffset(horizontal: 30, vertical: 30), forBarMetrics: UIBarMetrics.Default)
(Obj C)
[self.myBarItem setTitlePositionAdjustment:UIOffsetMake(30, 30) forBarMetrics: UIBarMetricsDefault];
Is this a bug with Appearance?
Change the baselineOffset of the text.
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.someFunction))
let attributes = [NSAttributedString.Key.baselineOffset: NSNumber(value: -3)]
doneButton.setTitleTextAttributes(attributes, for: .normal)
A negative baselineOffset will shift the title text down, a positive value will shift it up. 0 is the default value.
I tested this on iOS 11.
I was frustrated with this too so I subclassed UIBarButtonItem to work as expected. Taking from this answer I came up with this:
#interface TitleAdjustingBarButtonItem(){
UIView* _parentView;
UIButton* _button;
}
#end
#implementation TitleAdjustingBarButtonItem
-(id) initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action{
_button = [[UIButton alloc] init];
[_button setTitle:title forState:UIControlStateNormal];
[_button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[_button sizeToFit];
_parentView = [[UIView alloc] initWithFrame:_button.bounds];
[_parentView addSubview:_button];
return [super initWithCustomView:_parentView];
}
#pragma mark - property setters -
-(void) setTitlePositionAdjustment:(UIOffset)adjustment forBarMetrics:(UIBarMetrics)barMetrics{
[_button sizeToFit];
_parentView.bounds = CGRectMake(0.0, 0.0, _button.bounds.size.width - (adjustment.horizontal * 2.0), _button.bounds.size.height - (adjustment.vertical * 2.0));
}
Admittedly it does not account for the different bar metrics but for default metrics I've gotten this to work pretty well.
I found a workaround. I was trying to change the position of the back button title in the navigation bar. The title seems to be fixed in the lower left corner of it. So I changed the position of the whole navigation bar and adjusted its size accordingly so it would seem to be the same size in the view.
let xDisplacement: CGFloat = -25 // Adjust horizontal position
let yDisplacement: CGFloat = 9 // Adjust vertical position
let h = (self.viewIfLoaded?.frame.size.height)!/12 //Set your height in reference to the size of the view
let w = (self.viewIfLoaded?.frame.size.height)!//Set your width in reference to the size of the view
//Change placement adjusting size for change.
self.navigationController?.navigationBar.frame = CGRect(x: xDisplacement, y: yDisplacement, width: w-xDisplacement, height: h-yDisplacement)
In my case the background is transparent so the adjusting doesn't really make a difference. Also I was trying to move the title left. I think that is the only situation where de adjusting is useful.
Anyway, just a workaround, but I hope it helps.
Total noob here, feedback appreciated.
Suggestion for this: Use UIButton inside of UIBarButtonItem.
I have a toolbar that has two taller buttons in it on either side (Up Button and Down Button in the screenshot) and when the user initiates an action I show two buttons in the toolbar that only have text on them. When I originally put them on the toolbar as UIBarButtonItem objects, they sat very low on the toolbar and nothing I tried would get them to sit higher. Their position made it hard for the user to press them on iPadPro devices because the bar at the bottom of the device was kind of blocking them.
I found that if I dragged a UIButton to the toolbar, IB would automatically put those inside a UIBarButtonItem as the view, and they were centered vertically in the toolbar. I was then able to adjust the content Insets for the UIButton like I normally would and the change was reflected properly. (Since I was only looking to get them centered vertically, merely using UIButtons worked for me). Remember that any IBActions you hook up should be hooked up to the UIButtons.
I did this with XCode 10.1 and swift 4.
I am doing one iphone app. In this app i want to disable the back navigationleftbarbutton. I have used this below code for disable the navigationleftbarbutton but it is not working. Now i have created one view and i have added in uinavigationbar. But now i want to make that view in transparent view. And also how can i remove my customview(myview1) from the navigationbar. I have attached my code. please some body help me to solve my issue.
Disable Code:
self.navigationItem.leftBarButtonItem setEnabled:NO];
And This is my customview Code:
myview1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
myview1.backgroundColor = [UIColor redColor];
button = [[UIBarButtonItem alloc]initWithCustomView:myview1];
self.navigationItem.leftBarButtonItem = button;
How can i make myview is transparent view. And how can i remove mycustomview(myview1) from navigationbar. Thanks in advance.
you can hide navigation back button by writing code below:
self.navigationItem.leftBarButtonItem.hidden = YES;
and your view ie myview can be transparent by changing alpha value of the view
[myview setAlpha:0.3]; //alpha value ranges from 0 to 1
By settign alpha value 0.0 makes view transparent and increases transparency as u increases alpha value
I have a UILabel and I would like to make it react to a touch. I tried putting a button on top of the label, thanks to this I could interact with the button. However, the button cannot be fully transparent, right? I could set an alpha of the button to 0,02, but it is still visible on by background. How to solve this? Maybe I could set the properties in some other way to make them fully invisible? Or is there some other solution?
First, why not just use a button and set the button title to the label's contents?
If you can't/don't want to do that, you can also set userInteractionEnabled = YES on the label and then add a gesture recognizer to the label.
In Swift:
label.userInteractionEnabled = true
let gestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("labelPressed"))
label.addGestureRecognizer(gestureRecognizer)
Get your click in Action :
func labelPressed(){
print("Label pressed")
//Your awesome code.
}
I usually do this:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pushAction)];
[myLabel addGestureRecognizer:tap];
I don't know if it works with labels, but then i just make a transparent UIView with the same rect and put it on top.
Okay, i checked, it only works in UIView, but then, do this:
UIView *tapView = [[UIView alloc] initWithFrame:myButton.frame];
And put "tapView" in the addGestureRecognizer-method.
I create a rightBarButtonItem with this method :
- (UIBarButtonItem *)customBarButtonWithSelector:(SEL)callback {
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
customButton.bounds = CGRectMake(0, 0, 30.0f, 30.0f);
return [[[UIBarButtonItem alloc] initWithCustomView:customButton] autorelease];
}
At execution time the selector is fired when the bar is touched outside the button (near the middle).
Is there a way to restrict the event responder in the defined bounds or in a acceptable range ?
Try putting a fixed space before the button in the array of bar button items.
I have found the solution at http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
Really helpfull, don't forget to subclass UIToolBar.
hopefully someone can help me out- iv'e scoured the net for the answer and cannot find one-
the UIBarButtonItems added to UINavigationBar have a much larger click area than required-
for example, open up any project you have a nav bar with buttons on- click anywhere between the end of the button and the title of the nav bar- the button clicks, when you clearly did not click on the button-
also try this- click underneath the nav bar, below the button, the button clicks for about 5+ pixels below the nav bar-
my problem is this-
i have added a custom header with buttons to a tableview- but when i click the buttons in the header, the UINavigationBar buttons trigger for those 5+ pixels instead of the buttons in the tableview header-
i did a test, and removed the buttons from UINavigationBar and what is interesting is that for the 5 pixels below the nav bar, the buttons in the header will not trigger even though there are no buttons in the nav bar-
its almost like the nav bar has reserved some 5+ pixels below itself as click space-
my question is this-
can someone tell me how to make the nav bar not grab those extra 5+ pixels for its buttons?
thanks very much ;)
This is the only solution I found. Create a container of the custom button:
//Create a container for the button
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 44)];
//Create a smaller button
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 25)];
[closeButton setTitle:#"Cancel" forState:UIControlStateNormal];
//center the title
closeButton.titleEdgeInsets = UIEdgeInsetsMake(23, 0, 0, 0);
[buttonContainer addSubview:closeButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
I'm not 100% sure but maybe you can get the location of the touch via the UITouch class?
UIBarButtonItem doesn't extend UIResponder but UINavigationBar does!
So if you subclass UINavigationBar and use this subclass in your app, maybe you can catch the coordinates of the touch and check if they are ok to you and then decide to apply either the navigation bar action or your button action (by some custom redirection).
Short answer is... should shouldn't try and get rid of them. It's about ease of use. The navigation bar at the top tends to mean people tap lower than you may expect. Always leave that gap there, or have a sufficiently large hit area that the user stabbing their finger towards the middle of your "below the nav bar" item will avoid the dead area.
As far as I know, it is impossible to turn off. If you have other buttons on the navigation bar, those click-spaces will not collide, but if you have button directly beneath the nav bar with no space at all in between, you're out of luck. Consider a small padding in the header and its buttons as a solution.
Trying to work around the UINavigation Bar padding may run you into trouble when you submit to the app store. It would be easier to add the padding to your custom heading. As a "fat thumber" I have learned to appreciate the HIG.
Quite old question, but maybe a solution is helpful to others too...
I've created a UINavigationBar subclass, that overrides just one method: 'hitTest:withEvent:'. When hitTest:withEvent is called, it checks wether the event has happened inside the frame of the navigation bar (pointInside:withEvent:) or not. In case, the event has happened outside, the userInteractionEnabled flag is set to NO so the event will be ignored by the navigation bar and its subviews.
In my case, the navigation bar subclass is inserted via IB, but of course is is also possible to insert it via 'UINavigationController initWithNavigationBarClass:toolbarClass:'
Header:
#interface MMMasterNavigationBar : UINavigationBar
#end
Implementation:
#implementation MMMasterNavigationBar
/*
hitTest:withEvent:
The hit area in for navigation bar button items is enlarged by default.
Other objects directly below the navigation bar doesn't receive tap events.
We avoid the default enlarging of the tappable area by disabling userInteraction
when the real tap is outside the navigation bar.
*/
-(UIView *)hitTest:(CGPoint)pPoint
withEvent:(UIEvent *)pEvent {
//FLog;
if ([self pointInside:pPoint
withEvent:pEvent]) {
//NSLog(#"User interaction enabled");
self.userInteractionEnabled = YES;
} else {
//NSLog(#"User interaction disabled");
self.userInteractionEnabled = NO;
}
return [super hitTest:pPoint
withEvent:pEvent];
}
#end
This can be done directly from a storyboard. Drag a UIView into each navigation item, set its background to clearColor, and size it. Drag a button into each UIView and size them to match.
var buttonContainer:UIView = UIView()
buttonContainer.frame = CGRectMake(0, 0, 32, 32)
var imagess:UIImage = UIImage(named: "noti#2x.png")!
var closeButton:UIButton = UIButton()
closeButton.setImage(imagess, forState: UIControlState.Normal)
closeButton.frame = CGRectMake(10, 5, 20, 20)
closeButton.contentMode = UIViewContentMode.Center
closeButton.titleEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0)
buttonContainer.addSubview(closeButton)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonContainer)