2

I have this code:

JMenuController *menuController = [[JMenuController alloc] init];
    NSArray *buttonsArray = [NSArray arrayWithObjects:@"From Libary", @"Take Photo or Video", nil];
    [menuController showMenuWithTitle:@"Add Media" ButtonTitles:buttonsArray animated:YES];
    self.currentMenuType = JCreateMenuTypeNewMedia;

    [[[[UIApplication sharedApplication] delegate] window] addSubview:menuController];

The problem is, the view appears below the keyboard, which is already on screen. How do i fix this?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Andrew
  • 15,935
  • 28
  • 121
  • 203

3 Answers3

7

The easiest way to display some view on top of the keyboard is put that view inside a UIWindow for which you set the windowLevel property to UIWindowLevelAlert. Something like:

UIView *myView = [[UIView alloc] initWithFrame:rect];

UIWindow *myWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
myWindow.windowLevel = UIWindowLevelAlert;
[myWindow addSubview:myView];

[myWindow makeKeyAndVisible];

If you're using ARC, make sure you keep a strong reference to myWindow. Otherwise, it'll get automatically released and so it won't get added to your application windows.

(This is how UIActionSheet and UIAlertView work internally. When one of them is visible, have a look at [UIApplication sharedApplication].windows and you'll see they create windows with their windowLevel set to UIWindowLevelAlert.)

Ken M. Haggerty
  • 24,902
  • 5
  • 28
  • 37
samvermette
  • 40,269
  • 27
  • 112
  • 144
  • Thanks! then I also need to hide it by `[myWindow setHidden:YES];` – Nianliang Nov 13 '12 at 06:50
  • Thanks! This worked for me but once I dismiss the view I am not able to interact with my UI. Also there is a starange behaviour in Keyboard. My focus is still in UItextField & if I press any key there is no change in my textfield, but if I press backspace my text gets deleted. – JiteshW May 09 '13 at 09:45
1

The keyboard exists in its own window. To put things over it, you either have to put things into the keyboard's window (this is not a supported behavior), or create yet another window and put it over the keyboard.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
1

Ok it took awhile for me to understand what you wanted to do. You have 2 options.

  • Resign the keyboard and show the view with the buttons.

  • Use a UIActionSheet to display the buttons over the keyboard like it does in the Messages application when you press the camera to send a picture to someone.

I would recommend the later.

Edit: Take a look at this for your custom UIActionSheet Show a view over the keyboard

Community
  • 1
  • 1
Bot
  • 11,868
  • 11
  • 75
  • 131
  • I essentially have made my own action sheet, rather than use the default one. So i want to copy the behaviour of UIActionSheet. – Andrew Mar 02 '12 at 01:06