Navigation button click outside - objective-c

I created custom navigation buttons like this:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"some.png"] forState:UIControlStateNormal];
....
[[current navigationItem] setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:button]];
Where current is UIViewController * type.
All works fine and button created, but it's clickable area outside the button, very close to the middle of the navigation bar. Is it possible to limit clickable area?

I believe thats a "feature" of iOS. THe Navigation Buttons on left and right are smaller than the minimum touch area allowed in iOS. As a result the actual hit zone is much larger than the physical NavigationButton. Including just under the bar, and like you're noticing to the left and right of the button. Its to allow quick touches without having "look" where you're touching. Its one of the key reasons iPhones are more natural to use than most android phones in the early days.

My best guess is that the button is set to center the image and not scale it, so the frame of the button is way too big.
button.frame=CGRectMake(x,y,w,h)
Set the frame to what you want the clickable area to be.

I have initially thought about subclassing the UIBarButtonItem and override -touchInside:.
This does not work though, since UIBarButtonItem is not a subclass of UIView.
What you are trying to achieve is therefore not possible without overriding some private API.

Related

Remove focus from UIButton in tvOS

So I'm writing my first tvOS App (Objective C) and am having some fun with the "Focus Engine". My app is a 2 page app with a Tab Bar controller, on the main page I have a few UIButtons. On app startup if I hide the tab bar, the buttons look like I am wanting them to with them all deselected, when I swipe down one of the UIButtons obviously gets focus, and I can swipe between my various buttons, and after a specified amount of inactivity time I want it to go back to them all being unfocussed.
I start (and reset) a NSTimer when each UIButton gets focus and my intention is to remove the UIButton focus after say 10 seconds (there is a good reason for this, and it makes sense in my app / ui).
I've tried issuing a "UIButton resignFirstResponder" I've also tried to move focus back to the hidden TabBar, I even tried "preferredFocusEnvironments" but I cannot get the button highlight to come away. I also tried cycling though the buttons setting them all to "userInteractionEnabled = NO" then back again but the button retains focus. I have log lines showing the timer starting and it triggering my un-focus method when it expires, but no matter what I put in there I can't seem to get the focus to disappear.
Any ideas on how to drop the focus from a UIButton, I think part of the problem is I don't want to move it to somewhere else. I want to remove all button focus which I guess is an unusual thing to do.
Thanks in advance.
Plasma
I discovered a way to do it, its a bit primitive but it works and achieves the desired effect.
When the idle timer expires I create a UIButton (Custom Type) at 0,0 that is 1px high and the width of the screen. I then tell the view it needs a focus update, and to update the focus. This takes the focus from any of the main buttons and up to my 1px high button along the top of the screen.
[focusButton removeFromSuperview];
CGRect frame = CGRectMake(0,0,(self.view.frame.size.width),1);
focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
focusButton.frame = frame;
focusButton.tag = 99;
[self.view addSubview:focusButton];
[self.view setNeedsFocusUpdate];
[self.view updateFocusIfNeeded];
I then use 'didUpdateFocusInContext' to know when my 1px button has been given focus and set the button to disabled.
if(context.nextFocusedView.tag == 99){
NSLog(#"Focus Button Has Focus");
focusButton.enabled = NO;
}
This leaves the focus on that button which allows someone to swipe down to get to the main buttons, or up to get to the Tab Bar, once they swipe off it the 1px button is no longer selectable because is not enabled! I had to use Custom button type because system button type showed a white line over the top of the screen and tainted my labels.
Plasma

UIAppearance for UINavigationBar ios8

In my application currently I have implemented custom view in place of UINavigationBar. But there are 14-15 screens. So I repeatedly created that top view fro each screen.
So while surfing for the solution to avoid this repetition. I came across the concept of UIAppearance. But I don't know how to implement it. I have also gone through some basic concept of it but still I am very confuse.
My top view is bit different for each screen. Like in 4-5 screens there is Two buttons(back button on left and call button right) with one label in center.
Then in other 4-5 screens there is two buttons(cancel button on left and call button on right) with one label in center.
Then in other 2-3 screens there are two buttons(side menu button on left and call button on right) with one label and icon in center.
Can anyone tell me how to achieve it using UIAppearance?
Thanks in advance!
You can't set buttons via UIAppearance. If you are setting titleTextAttributes on each page (for say, font, size and color), you can do that via the UINavigationBar appearance like this:
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
The attributes pointer is a NSDictionary containing your textAttributes (NSFontAttributeName, NSForegroundColorAttributeName etc.)

Detecting partial touch on UIButton

I've created a UIButton and detect the touch event by:
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Everything works fine when I press exactly on the button.
My problem is that if I press with my finger part on the button and part outside - The event is not detected.
Is there another event I should use?
Am I doing something wrong here?
I'm sure people will say that you have to press exactly in the button but I'm trying to give the best user experience and resolve this issue.
Thanks!
You may want to enlarge your button, making perhaps a transparent area around your image.
Or maybe use UIControlEventTouchDown/UIControlEventTouchUpOutside instead of UIControlEventTouchUpInside ?

Objective c animating UIbutton like dock effect on mac os x

I am working on app where I have some list of content shown with some buttons . I need a dock kind of animation when I touches the button , then clicking on the docked button event should be fired. when I touches another button this one should be docked . I have seen the following like
http://praastitutki2012.wordpress.com/2009/11/23/mac-osx-dock-like-animation-for-iphone/
but , here I need to create a separate view for handling this . But I need this only for some of the buttons only , not for the entire view.
Any suggestions ?
Thanks in advance.
I didn't check in details the code you linked, but actually a UIButton is a UIView. You may consider subclassing UIButton and implement those methods. This way, animating the whole view means animating the button.

UIToolbar above UIWebView inside UIScrollView

I'd like to display a toolbar above a UIWebView but hide the toolbar until the person "pulls it down".
The same functionality can be seen in Safari on the iPhone. When the page loads, the toolbar containing the address is hidden. You must pull it down. In Safari it's possible to scroll up and eventually see the toolbar or scroll down through the page contents.
I've tried placing a UIToolbar and UIWebView inside a UIScrollView but it didn't work.
I've tried setting the UIScrollView to the size of the toolbar and webview combined, but that didn't work.
- (void)viewDidLoad{
CGSize size = CGSizeMake(webView.frame.size.width,
toolBar.frame.size.height + webView.frame.size.height);
[scrollView setContentSize:size];
}
How should I go about doing this?
EDIT
Anyone looking at this should consider that the question and accepted answer are both old and that the API might have changed to allow this.
The UIWebView is itself a UIScrollView so it's not going to work. I'm not sure how Apple does it, but one way to do it, if you have control over the content of the web view, is to write some HTML and CSS that replicates that address bar.