Can anyone explain how best to emulate the behaviour the "accessory view" (in inverted comma's as I don't actually believe it to be an accessory view) in iOS 5's Messages.app displays, in that I want a view that gives the impression it's fixed to the top of the keyboard, but which remains on screen when the keyboard is dismissed.
It's probably a separate view that gets re-positioned using an animation that has the same duration as the keyboard animation.
Try observing the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and in your handlers get the keyboard's animation duration and frame and start your own animation to re-position the view so that it will appear to move along with the keyboard. Some similar code I use is below:
- (void)registerKeyboardNotifications {
// Register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unregisterKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGRect kbFrameBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect kbFrameEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animCurve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSLog(#"\nFrame Begin = %#\nFrame End = %#\nAnimation Duration = %f\nAnimation Curve = %i",
NSStringFromCGRect(kbFrameBeginFrame), NSStringFromCGRect(kbFrameEndFrame), animDuration, animCurve);
_showKeyboard = YES;
[self adjustUIForKeyboard:kbFrameEndFrame.size animDuration:animDuration];
}
- (void)keyboardWillHide:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGRect kbFrameBeginFrame = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect kbFrameEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animCurve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSLog(#"\nFrame Begin = %#\nFrame End = %#\nAnimation Duration = %f\nAnimation Curve = %i",
NSStringFromCGRect(kbFrameBeginFrame), NSStringFromCGRect(kbFrameEndFrame), animDuration, animCurve);
_showKeyboard = NO;
[self adjustUIForKeyboard:kbFrameEndFrame.size animDuration:animDuration];
}
/**
* Adjust the UI elements so that views are visible when keyboard is visible or hidden
*/
- (void)adjustUIForKeyboard:(CGSize)keyboardSize animDuration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration
animations:^(void) {
// When keyboard is showing we adjust up and vice versa for a hidden keyboard
if (_showKeyboard) {
// Set your view's frame values
} else {
// Set your view's frame values
}
}
completion:NULL];
}
Related
Hi guys I am making a notes app and I ran into a big problem. I am using a UITextView as the notepad. When the keyboard comes up it blocks some of the text in the UITextView. I have a input accessory view on the UITextView. I tried to find the answer in the internet and can not find a good answer.Any way to fix it? Here is a picture:
You might want to look at modifying the UITextView's contentOffset and contentInset. UITextField is a UIScrollView subclass after all.
I decided to subtract the UITextView height by the keyboard height:
NSDictionary* info = [notification userInfo];
kbSIZE = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect newTextViewFrame = self.notesTextView.frame;
newTextViewFrame.size.height -= kbSIZE.size.height;
newTextViewFrame.size.height += self.notesTextView.inputAccessoryView.frame.size.height;
self.notesTextView.frame = newTextViewFrame;
You have to set contentInset and scrollIndicatorInsets to the UIEdgeInsets of the keyboard height. The contentInset value makes the scrolling height taller, but allows you to still scroll content under the keyboard. The scrollIndicatorInsets makes the scroll indicators stop at the bottom of the keyboard.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.textView.contentInset = contentInsets;
self.textView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
self.textView.contentInset = UIEdgeInsetsZero;
self.textView.scrollIndicatorInsets = UIEdgeInsetsZero;
}
I want my view to scroll down when keyboard hides, the thing is that for the first time the view comes from outside of its frame(first image I have attached) but if I hide keyboard for the second time the view scrolls down in a correct way(second figure).
Here is the code related to this part:
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[_scrollView setScrollEnabled:TRUE];
}
- (void)keyboardDidHide:(NSNotification *)notification
{
[UIView animateWithDuration:4 animations:^{
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size (without keyboard)---
CGRect viewFrame = [self.view frame];
viewFrame.size.height += keyboardRect.size.height;
self.scrollView.frame = viewFrame;
}];
}
So, as often suggested for people who want the view to be pushed up when the keyboard appears, I use a UIScrollView. I copied and pasted the information from this page -- http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 -- to do that...
But what happens is, when I tap the text fields, the keyboard pops up, but the view actually scrolls DOWN, revealing black on top. After that, I can actually manually scroll the view up and reveal the hidden text fields...obviously, that's not what I'm trying to do!
Any ideas? I have the following in the viewDidLoad() method:- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
I've tried this version of keyboardWasShown:
- (void) keyboardWasShown:(NSNotification *)aNotification
{
NSDictionary * info = [aNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0,keyboardSize.height, 0);
[[self introScroll] setContentInset:contentInsets];
[[self introScroll] setScrollIndicatorInsets:contentInsets];
CGRect ltdRect=[[self view] frame];
ltdRect.size.height=keyboardSize.height;
if (!CGRectContainsPoint(ltdRect,[self activeField].frame.origin))
{
CGPoint scrollPoint = CGPointMake(0,[self activeField].frame.origin.y-keyboardSize.height);
[[self introScroll] setContentOffset:scrollPoint animated:YES];
}
NSLog(#"Keyboard out!");
}
as well as this version of keyboardWasShown:
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = [[[self activeField] superview ]frame];
bkgndRect.size.height += kbSize.height;
[[[self activeField] superview] setFrame:bkgndRect];
[[self introScroll] setContentOffset:CGPointMake(0.0, [self activeField].frame.origin.y-kbSize.height) animated:YES];
}
Both give me the exact same results.
What's going on?? I'm using Xcode 4.6.
I had a similar problem that my text fields did not scroll into view.
the apple code is not perfect, I had to modify it:
Try to solve that by adding the height of active field in line:
frame.origin.y + frame.size.height - keyboardSize.height
If that does not work, add 44 to scrollpoint.y (size of navigation bar)
Update for ios7:
I improved unwanted and wrong scrolling in ios7 for text fields, that are already in view
by
if (scrollPtY < 0) {
return;
}
I want to create a view, like the Facebook or Twitter app's share dialog, for example, where there is just a UITextView and a permanent keyboard. I can see how to start with the keyboard visible from this answer, but I'm not sure how to size the UITextView to fit exactly above the keyboard. If I don't, text can get hidden under the keyboard which is awkward.
You can subscribe to UIKeyboardWillShowNotification. The notification includes the frame of the keyboard, so you can size your view to fit the remaining space.
I found a helpful code sample in the Apple documentation for this: https://developer.apple.com/library/ios/#samplecode/KeyboardAccessory/Introduction/Intro.html
Here's the code I ended up with. I'm not worrying about the keyboard hiding, since in my view, the keyboard should never hide.
- (void)viewWillAppear:(BOOL)flag
{
[super viewWillAppear:flag];
// Listen for the keyboard to show up so we can get its height
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// Set focus on the text view to display the keyboard immediately
[self.textView becomeFirstResponder];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* keyboardFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's
// coordinate system. The bottom of the text view's frame should align with the
// top of the keyboard's final position.
CGRect keyboardRect = [keyboardFrame CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
// Set the text view's frame height as the distance from the top of the view bounds
// to the top of the keyboard
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
self.textView.frame = newTextViewFrame;
}
You can get the keyboard size like so :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardIsUp:) name:UIKeyboardDidShowNotification object:nil];
- (void)keyboardIsUp:(NSNotification *)notification{
CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
NSLog(#"%f", keyboardSize.height);
}
[EDIT] Landscape mode
I tried it on the iPas Simulator and it return 264 in portrait mode for a QWERTY keyboard but when you start the app or rotate to landscape mode it returns 1024. So you might need to ask for the width instead of the height in landscape mode...
[EDIT]
Thanks to rob mayoff's comment there is no problem with the landscape mode anymore
[EDIT]
This is not the best way of doing it, but that gives an idea. I'll take a look back at it later
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
CGFloat width = size.width;
CGFloat height = 40;
CGFloat x = 0;
CGFloat y = size.height+40;
aboveKBView = [[[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)] autorelease];
[aboveKBView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:aboveKBView];
}
- (void)keyboardWillHide:(NSNotification *)notification{
[aboveKBView setHidden:YES];
}
- (void)keyboardWillShow:(NSNotification *)notification{
NSLog(#"keyboardIsUp");
CGSize keyboardSize = [self.view convertRect:[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue] toView:nil].size;
CGSize size = [self.view convertRect:self.view.frame toView:nil].size;
CGFloat width = size.width;
CGFloat height = 40;
CGFloat x = 0;
CGFloat y = size.height-(keyboardSize.height+height);
[aboveKBView setFrame:CGRectMake(x, y, width, height)];
[aboveKBView setHidden:NO];
}
I find this page always quite helpful - it shows all important sizes, so it's easy to calculate the size you need:
http://www.idev101.com/code/User_Interface/sizes.html
Try this
in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
and add these methods.
- (void) keyboardWillShow: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y -= 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
- (void) keyboardWillHide: (NSNotification*) aNotification;
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = [[self view] frame];
rect.origin.y += 60;
[[self view] setFrame: rect];
[UIView commitAnimations];
}
I am using keyboardWasShown and keyboardWillBeHidden notification for sliding the view to get the visible text view.
I have a UITabBar application with six tabs.
In each view I am using the UINavigationController.
In the detail view of each UITableViewCell I am using the keyboard notifications.
So the problem is that the keyboard notifications work for the first time that I will use . on on the other tabs it won't work .
The code is as follows :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasHidden:)
name:UIKeyboardDidHideNotification
object:nil];
and the methods
- (void)keyboardWasShown:(NSNotification *)aNotification {
if ( keyboardShown )
return;
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y -= keyboardSize.height-100;
frame.size.height += keyboardSize.height-100;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = YES;
keyboardShown = YES;
}
- (void)keyboardWasHidden:(NSNotification *)aNotification {
if ( viewMoved && tvAddreview) {
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
NSTimeInterval animationDuration = 0.300000011920929;
CGRect frame = self.view.frame;
frame.origin.y += keyboardSize.height-100;
frame.size.height -= keyboardSize.height-100;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
viewMoved = NO;
}
keyboardShown = NO;
}
you should dothis in eachClass like this:
-(void) viewWillAppear: (BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[nc addObserver:self
selector:#selector(keyboardWasHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void) viewWillDisappear: (BOOL)animated{
[super viewWillDisappear:animated];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[nc removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
because the notifications are on the application level not to your class level. So if you have added them in one class and not in all classes, then going to the next class. the notification will still call the the key keyboardWasShown: and the other from the class in which you added the notifications hence your local variables like...
viewMoved = YES;
keyboardShown = YES;
will throw the bad excess exceptions
In your case it is also required to do in all 6 view controllers
Hope this helps.