9

I'm currently fixing an issue in an old app (built in the days of iOS 5) where button touch events are ignored at the bottom of the screen. I am able to recreate this issue by creating a new project, deleting the storyboard (and the reference in the Info plist), and doing the following in didFinishLaunchingWithOptions (I know this isn't the correct way to do, but this app i'm working on has a similar setup)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    self.window.screen = [UIScreen mainScreen];

    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    self.window.rootViewController = vc;

    UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [test setTitle:@"Hey" forState:UIControlStateNormal];
    [test setBackgroundColor:[UIColor whiteColor]];
    test.frame = CGRectMake(0, 824, 200, 200);
    [vc.view addSubview:test];

    [self.window makeKeyAndVisible];

    return YES;
}

Once I run this app, the "Hey" button only works by depressing the upper half of it. I have a feeling it's related to the iOS 7 status bar, and how the UI now goes underneath it rather than below it. I tried adjusting the window's bounds, but that doesn't help. I've tried all sorts of things to get this button to work, but the only thing that works is hiding the status bar.

Anyone have any clues how to get that button working in this situation?

veducm
  • 5,933
  • 2
  • 34
  • 40
Felix Khazin
  • 1,479
  • 2
  • 10
  • 16
  • Excuse the hardcoded values, i'm testing with an iPad Mini, and just wanted to create something quick and dirty. – Felix Khazin Jan 23 '14 at 19:38
  • And one last thing...this works fine on the Simulator, but on the actual device it doesn't... – Felix Khazin Jan 23 '14 at 19:39
  • You are probably right and it has to do with the status bar. Please read [this answer](http://stackoverflow.com/a/18855464/1264909) to see if it helps you figure out how to avoid this problem. – veducm Jan 27 '14 at 16:19
  • Could you provide the actual drawing code you're having problems with? From your test I don't really know what you're doing other than adding hard coded framed views... which doesn't tell me much and doesn't work for me. iOS7 introduced Autoresizing masks that really overhauled their entire layout methodology / structuring, my iOS 6 built apps didnt place button views correctly so while they looked correct the touch events registered in the containers half the screen away. Try adding this [test setTranslatesAutoresizingMaskIntoConstraints: NO]; – Jeremy Feb 04 '14 at 06:42
  • @FelixKhazin - Hey have you checked Autolayout is turned on or not – bhavya kothari Feb 04 '14 at 09:12
  • @Jeremy "iOS7 introduced Autoresizing masks". So wrong. Autoresizing mask where introduced from the start in iOS 2.0. You're thinking about Autolayout, which was introduced in iOS 6.0. – Guillaume Algis Feb 04 '14 at 19:15
  • @Guillaume Auto layout, auto resizing masks, whatever they changed about their function in iOS 7 affected how items were displayed for me - similar: http://stackoverflow.com/q/19096496/1709073 – Jeremy Feb 04 '14 at 23:27
  • @Jeremy not sure what you mean by actual drawing code. Create a new iOS project. If you have an iPad mini use my values, if not replace with values for your iDevice. And copy this code into didFinishLaunchingWithOptions. Run it on the actual device and you will notice the bottom portion of the button is not clickable. – Felix Khazin Feb 08 '14 at 18:15

5 Answers5

3

I don't think the events are being ignored - I think it's just that the button highlight feedback isn't being displayed for button presses at the bottom of the screen in iOS7

Using your example, add this to didFinishWithLaunching in AppDelegate.m

[test addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

And then add this method to AppDelegate.m

-(void)buttonPressed:(id)sender{
    NSLog(@"hey!");
}

Tap at the bottom edge of the button and you'll see the action method called even though the visible feedback isn't shown.

Oddly, if you press and drag in any direction the visual feedback shows correctly.

john
  • 31
  • 1
0

I tried your code in an ipad app and the 'test' button showed up only in portrait mode.

I have used the following code to make it appear in landscape and also added a target for its action.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
{
 self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
 self.window.screen = [UIScreen mainScreen];

 UIViewController *vc = [[UIViewController alloc] init];
 vc.view.backgroundColor = [UIColor yellowColor];
 self.window.rootViewController = vc;

 UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 test.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
 [test setTitle:@"Hey" forState:UIControlStateNormal];
 [test addTarget:self action:@selector(testTapped:)  forControlEvents:UIControlEventTouchUpInside];
 [test setBackgroundColor:[UIColor whiteColor]];
 test.frame = CGRectMake(0, 824, 200, 200);
 [vc.view addSubview:test];

 [self.window makeKeyAndVisible];

 return YES;
}

-(void)testTapped:(id)sender
{
 // the button responder
}

Autoresizing mask is necessary in this case since you have hardcoded frames for the button. I have added 'UIViewAutoresizingFlexibleTopMargin' makes it visible in both landscape and portrait.

Also added a target for the button action, which is necessary for the button to respond.

Hope it helps.

Anki
  • 23
  • 3
  • This doesn't solve the problem. Load this app on your device. Then try to click the very bottom of the "Hey" button. You will see that tapping there does not work. – Felix Khazin Feb 05 '14 at 14:50
0

when will i tested your code my simulator button not visible to it simple show only yellow. because the reason is your button y position is create beyond visible screen.

your button y value 824.but device size 568(iPhone 4 inch). so your button create beyond current visible screen bounds

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    self.window.screen = [UIScreen mainScreen];

    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    self.window.rootViewController = vc;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(aMethod:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [vc.view addSubview:button];

    [self.window makeKeyAndVisible];

    return YES;
}
codercat
  • 22,873
  • 9
  • 61
  • 85
  • While it's true the button will not be visible on an iPhone, it's not actually the point of my issue. Take the button you have, and move it right to the bottom edge of your iPhone. So set y=(568-40). Now click the bottom 20 pixels of the button. They aren't clickable...That's the problem. – Felix Khazin Feb 08 '14 at 18:12
0

I have run your code in test project. After run this project I did not got your button, because it was out of frame. Then I set frame properly, then the button appears and it receives action. I am suspecting that your button was out of frame that's why it was not receiving the action. Please check the view frame and make sure button is inside the view, not outside of view.

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
  • As i mentioned in previous comments, just adjust the button so it's at the bottom of the screen and you will notice 20 pixels of the button are not clickable. – Felix Khazin Feb 10 '14 at 16:25
  • Can you please upload your project and give me the link, so that I can figure it out what is happening. – Abdullah Md. Zubair Feb 10 '14 at 17:29
0

"I tried adjusting the window's bounds"

The button do not lay where you think it does. It is out of bounds, that is why you can not press it properly. You can only press buttons inside the the view and not buttons outside the view even though the button will be shown on the screen.

You could remove the statusbar unless you want it...

Remember you should adjust the button inside the view. Draw this on paper, draw the phone, do not have to be accurate but you need to know all sizes of objects. Draw inside the status bar, how much space do you have left. If you then set the frame size to be larger than the remaining size it will go out of bounds. When it comes to the button you should try to not use static data for placement, but use the sizes of the view to determine where the button should be inside the view.

- (void)applicationDidFinishLaunching:(UIApplication *)application{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// Override point for customization after application launch.

mvc = [[MainViewController alloc]init];
[mvc setTitle:@"Main View"];

UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[test setTitle:@"Hey" forState:UIControlStateNormal];
[test setBackgroundColor:[UIColor whiteColor]];
float buttonHeight = 200;
float buttonWidth = 200;
float statusbarHeight = 20; //can be adjusted to 0 if no statusbar [ 20 | 0 ]
test.frame = CGRectMake(0, mvc.view.frame.size.height-buttonHeight-statusbarHeight , buttonWidth, buttonHeight); //Will adjust with different screen sizes
[mvc.view addSubview:test];

[self.window addSubview:mvc.view];
self.window.rootViewController = mvc;
[self.window makeKeyAndVisible];
}
Odd-Arne
  • 111
  • 1
  • 3