6

I am looking for a way on the iPhone, due to a custom 'loading' subview, to have a subview cover the keyboard without dismissing the keyboard. Right now when the subview loads, the keyboard is still topmost. Any ideas?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Jake Sankey
  • 4,977
  • 12
  • 39
  • 53
  • Whenever You are adding subview to the current View just write this code before adding subview [TextView resignFirstResponder]; as you are opening keyboard from UITextView. – Mehul Mistri Jul 01 '11 at 05:49

3 Answers3

19
@interface UIApplication (Util)

- (void)addSubViewOnFrontWindow:(UIView *)view;

@end

@implementation UIApplication (Util)

- (void)addSubViewOnFrontWindow:(UIView *)view {
    int count = [self.windows count];
    UIWindow *w = [self.windows objectAtIndex:count - 1];
    [w addSubview:view];
}

@end


UIApplication *app = [UIApplication sharedApplication];
[app addSubViewOnFrontWindow:_loadingView];
Kota Mizuguchi
  • 191
  • 1
  • 3
13
UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
[window addSubview:self.view];                                                                                                                                                                                                                                        
[window bringSubviewToFront:self.view];
Artyom
  • 1,099
  • 15
  • 18
7

add your loading view as subview on window. It will cover keyboard too. Here is a stackoverflow post for the same

IPhone - Show Wait Indicator

UPDATE

My code

#pragma mark -
#pragma mark Waiting View
- (void)showWaitingView {

    CGRect frame = CGRectMake(90, 190, 32, 32);
    UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
    [progressInd startAnimating];
    progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;

    frame = CGRectMake(130, 193, 140, 30);
    UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
    waitingLable.text = @"Processing...";
    waitingLable.textColor = [UIColor whiteColor];
    waitingLable.font = [UIFont systemFontOfSize:20];;
    waitingLable.backgroundColor = [UIColor clearColor];
    frame = [[UIScreen mainScreen] applicationFrame];
    UIView *theView = [[UIView alloc] initWithFrame:frame];
    theView.backgroundColor = [UIColor blackColor];
    theView.alpha = 0.7;
    theView.tag = 999;
    [theView addSubview:progressInd];
    [theView addSubview:waitingLable];

    [progressInd release];
    [waitingLable release];

    [window addSubview:[theView autorelease]];
    [window bringSubviewToFront:theView];
}

- (void)removeWaitingView {
    UIView *v = [window viewWithTag:999];
    if(v) [v removeFromSuperview];

}
Community
  • 1
  • 1
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • Thanks for the link. Actually MBProgressHUD is what I am trying to implement. however, I want the keyboard to always remain visible and MBProgressHUD is behind the keyboard. – Jake Sankey Jul 01 '11 at 05:56
  • @Jake Sankey You can add mbprogresshud to window instead of current view.I'm posting a simple code to display a activity indicator. You can use that. – Rahul Vyas Jul 01 '11 at 06:18
  • there is no 'window' in my implementation file. it is in the app delegate. i tried [super.view.window addSubview:HUD]; with no luck ... – Jake Sankey Jul 01 '11 at 07:38
  • 1
    you have window in applicationDelegate. From app delegate you need to call this function. – Rahul Vyas Jul 01 '11 at 11:21
  • I'm sorry Rahul, I don't understand what you mean. What function am I calling? I am trying to load the MBProgressHUD over my viewcontroller so the appdelegate (which defines 'window') doesn't seem accessible. – Jake Sankey Jul 01 '11 at 13:29
  • you can access appDelegate anywhere in the application using the below code yourAppdelegateName *appDelegate = [UIApplication sharedApplication].delegate; By the way the code I have posted you need to paste into your appDelegate class. And then access like this. yourAppdelegateName *appDelegate = [UIApplication sharedApplication].delegate; //To display waiting View [appDelegate showWaitingView]; //To remove Waiting View [appDelegate removeWaitingView]; – Rahul Vyas Jul 01 '11 at 14:14
  • 6
    this doesn't seem to work with the iOS5 SDK. The keyboard is on top no matter what. – Jiho Kang Oct 12 '11 at 06:42