Xamarin - How to layer View on top of another, like Keyboard
Question
I have a custom UI I want to show like the keyboard when a user clicks a part of the screen. I've seen examples however they reference methods off of UIView which don't seem to exist (maybe it's because I have the Starter license?) for example UIView.beginAnimations isn't there.
Does anyone have a Xamarin (i.e. MonoTouch) example?
I want my UI piece to show, and to scroll the existing view so that the field is properly visible.
Solution
UITextView
and UITextField
have inputView
property, which allows you to achieve exactly what you need. Setup the view, set it as their input view, and once the user makes a text view or a text field first responder, your view will appear. iOS7 has UIInputView
class, which you can subclass, to achieve a similar background view as the keyboard.
OTHER TIPS
Why don't you create your view and place it outside the visible view. When you want to show it, you just move it into the visible area. That way you can also add the animation.
NSLayoutConstraint *yourHiddenViewYConstraint;
- (void)viewDidLoad
{
[self.view addSubView:yourHiddenView];
[yourHiddenView setHidden:YES];
UIView *selfView = self.view
yourHiddenView.translatesAutoresizingMaskIntoConstraints = NO;
// add your needed constraints, other than the one below
yourHiddenViewYConstraint = [NSLayoutConstraint constraintWithItem:yourHiddenView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:selfView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0];
[selfView addConstraint:yourHiddenViewYConstraint];
yourHiddenViewYConstraint.constant += self.view.frame.size.height;
}
To show it when needed:
- (void)showView
{
[yourHiddenView setHidden:NO];
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self changeConstraint:yourHiddenViewYConstraint withConstant:-self.view.frame.size.height];
});
}
- (void)changeConstraint:(NSLayoutConstraint *)constraint withConstant:(CGFloat)constant
{
constraint.constant += constant;
void (^anim)(void) = ^{
[self.view layoutIfNeeded];
};
[UIView animateWithDuration:0.5 animations:anim completion:nil];
}