1

I have a UITableView with a search bar and a search result display controller:

[self.searchDisplayController.searchResultsTableView addSubview:searchIndicator];
searchIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);

The code above places the indicator at the center of the screen. However I want to place the indicator at the center of the frame excluding the keyboard. How can I do this?

The indicator is defined like this:

@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *searchIndicator;

enter image description here

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
cateof
  • 6,608
  • 25
  • 79
  • 153

3 Answers3

1

@SaurabhPrajapati has the right idea in his comment. You'll need to subscribe to one of the keyboard willShow/didShow notifications (UIKeyboardDidShowNotification or UIKeyboardWillShowNotification) and when you get a keyboard notification, collect information about the keyboard height from the Keyboard Notification User Info Keys (Search in the Xcode help system on that string for more information.) Save the keyboard height to an instance variable, and then when you get ready to display your activity indicator, use the keyboard height to adjust the position of the indicator as outlined in Saurabh's comment.

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
0

add this in viewDidLoad

//KeyBoard Helpers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];

Then implement this method

-(void)keyBoardWillShow:(NSNotification*)notification
{
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyboardFrame.size.height;
    searchIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height - keyBoardHeight/ 2.0);
}
Saurabh Prajapati
  • 2,348
  • 1
  • 24
  • 42
  • I understand the code, but it does not work. I think the problem is that the searchIndicator.center should take place in viewDidLoad. – cateof Sep 10 '15 at 12:56
  • In viewDidLoad please see the statement [self.searchDisplayController.searchResultsTableView addSubview:searchIndicator]; ie the searchIndicator is already placed as a subview – cateof Sep 10 '15 at 12:57
  • 1
    You can't reposition views in `viewDidLoad` because they haven't been laid out for the current screen size and orientation yet. You have to wait until `viewWillAppear` or `viewDidLayoutSubviews`. Plus your keyboard won't have been displayed yet. Do you have your view controller set up so the keyboard is shown as soon as the view controller is displayed? – Duncan C Sep 10 '15 at 13:14
0

You could just add a constraint to the top of your UIActivityIndicatorView and inside the to notifications of your keyboard. When keyboard will be shown, you shrink it by the keyboard height, if keyboard will be hide, you add the keyboard height again to the top constraint.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74