currently I'm attempting to basically implement and exact copy of Apples iMessage App.
That means I need a UITextView that is docked at the bottom of the screen, and moves up when it becomes firstResponder. - That's pretty easy actually. There's a bazillion ways to do that and two of the most common are of course animating the view upwards or downwards if a notification was received. The other is to do it via the inputAccessoryView. Sadly some of the features the one has, the other doesn't. And they seem to be mutually exclusive.
The big problem is rotation.
I've digged through roughly at least seven different github projects, all of them re-implementing the same functionality/behavior, that I'm trying to achieve, but literally all of them failing miserably.
HPGrowingTextView for instance, which the official Facebook/FacebookMessenger/(and possibly WhatsApp) Apps makes use of, is one big piece of junk-code. Take your iDevice, open the Facebook App, go the the Chat, pop the keyboard and rotate your device. If you pay attention you'll notice the input-bar jumping slightly and leaving some blank space between the keyboard's frame and its own. Then take a look at Apples implementation in iMessage when the keyboard is shown. It's perfect.
Other than that the contentOffset and EdgeInset-hacking that the HPGrowingTextView library makes use of gives me nightmares.
So I wanted to do it myself and start from scratch.
Right now I've got a very slick, elegant and hack-less implementation of a growing UITextView, but one part is missing.
Elegant rotation.
When I simply adjust the frames to their respective new positions in the willRotateToInterfaceOrientation:duration: method, everything ends up working perfectly BUT I have the same problem that HPGrowingTextView(see Facebook App) has. A litte bit of space between the inputview and the keyboard while the rotation takes place.
I found out that when rotating the device to landscape, the portrait keyboard which is currently shown does not "morph" but rather disappears (sends the 'willHide' notification) and a landscape version reappears (sending the 'willShow' notification). The transition is a very subtle fade and possibly some resizing.
I re-implemented my project using the inputAccessoryView to see what happens then and I was pleasantly surprised. The inputAccessoryView rotates in perfect sync with the keyboard. There's no space/gap between the two.
Sadly I have yet to come up with an idea how to have the inputAccessoryView dock to the bottom of the screen and NOT disappear/move out of it alongside the keyboard...
What I don't want are hack-y solutions like,..."lowering the frame slightly in the toInterfaceOrientation's CoordinateSystem and then moving it back up when the didRotateFrom... was called."
I know of one other app that has managed to implement such behavior and it's the "Kik Messenger".
Does anyone have an idea, advice or a link that I haven't seen yet covering that topic?
Thanks a bunch!
Note: Once this problem is solved I will open source the project for everyone to profit because almost every implementation I was able to find over the course of the past few days, is a mess.
I recently ran into the same problem, and had to build out a custom solution as I wasn't entirely happy with the available 3rd party libraries. I've split out this implementation into it's own GitHub project:
MessageComposerView
From some simple testing on iOS 6.1 7 & 8 simulators the rotations seem to properly follow the keyboard. The view will also grow with text and resize automatically on rotation.
You can use a very basic init function like so to create it with screen width and default height e.g.:
self.messageComposerView = [[MessageComposerView alloc] init];
self.messageComposerView.delegate = self;
[self.view addSubview:self.messageComposerView];
There are several other initializers that are also available to allow you to customize the frame, keyboard offset and textview max height. See readme for more!
I have been successful at solving the problem in quite an elegant manner (I think,...).
The code will be released on Github next week and linked to in this answer.
--
How it's done: I made the rotation work by choosing the inputAccessoryView-way of doing it.
Nomenclature:
'MessageInputView' is a UIView containing my 'GrowingUITextView' (it also contains a "Send" Button and the background image).
'ChatView' is the view that belongs to the ChatViewController that displays all the Chatbubbles and has my 'MessageInputView' docked at the bottom.
'keyboardAccessoryView' is an empty UIView sized: CGRect(0,0,0,0).
I needed to figure out how to have the MessageInputView stick around on the screen when the keyboard was dismissed. That was the tricky part. I did this by creating another view (keyboardAccessoryView) and had my GrowingUITextView use it as its inputAccessoryView. I retained the keyboardAccessoryView because I'd need the reference to it later on.
Then I remembered some of the stuff I did in my other attempt (animating the MessageInputView's frames around the screen whenever a keyboard notification arrived).
I added my MessageInputView as a subview to my ChatView (at the very bottom). Whenever it is activated and the willShow: methods is called by a keyboard notification, I manually animate the MessageInputView's frame to it's designated position up top. When the animation finishes and the completion block executes I remove the subview from the ChatView and add it to the keyboardAccessoryView. This causes another notification to be fired off because the keyboard is re-loaded EVERY time the inputAccessoryView's frame/bounds are changed!. You need to be aware of that and handle it appropriately!
When the keyboard is about to dismissed, I convert my MessageInputView's frame to my ChatView's coordinate system and add it as a subview. Thus it is removed from my keyboardAccessoryView. I then resize the keyboardAccessoryView's frame back to CGRect(0,0,0,0) because otherwise the UIViewAnimationDuration will not match! Then I allow the keyboard to be dismissed and I have my MessageInputView follow it from above and eventually dock at the bottom of the screen.
This is quite a lot of work for very little gain though.
--
Take care.
PS: If someone figures out an easier way to do it (perfectly) let me know.
Here's a UITextView subclass that is working properly on iOS 9.3.1 and 8.3.1. It takes care of growing and shrinking with limits, while keeping the caret always in the right place and animating smoothly.
Sticking the view over the keyboard is trivial, with many solutions to be found easily, so it's not covered...
I could not find any made-solutions that were production ready so I ended up working on this from scratch. I had to work out a lot of little problems along the way.
Code comments should give you an idea of what's going on.
I have shared this on my Github, Contributions greatly appreciated.
Notes
Not tested to support landscape
Not tested on i6+
Demo
(after max height element becomes scrollable. Forgot to drag the demo, but this is working as expected as well... )
Subclass
class ruuiDynamicTextView: UITextView {
var dynamicDelegate: ruuiDynamicTextViewDelegate?
var minHeight: CGFloat!
var maxHeight: CGFloat?
private var contentOffsetCenterY: CGFloat!
init(frame: CGRect, offset: CGFloat = 0.0) {
super.init(frame: frame, textContainer: nil)
minHeight = frame.size.height
//center first line
let size = self.sizeThatFits(CGSizeMake(self.bounds.size.width, CGFloat.max))
contentOffsetCenterY = (-(frame.size.height - size.height * self.zoomScale) / 2.0) + offset
//listen for text changes
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textChanged), name: UITextViewTextDidChangeNotification, object: nil)
//update offsets
layoutSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
//Use content size if more than min size, compensate for Y offset
var height = max(self.contentSize.height - (contentOffsetCenterY * 2.0), minHeight)
var updateContentOffsetY: CGFloat?
//Max Height
if maxHeight != nil && height > maxHeight {
//Cap at maxHeight
height = maxHeight!
} else {
//constrain Y to prevent odd skip and center content to view.
updateContentOffsetY = contentOffsetCenterY
}
//update frame if needed & notify delegate
if self.frame.size.height != height {
self.frame.size.height = height
dynamicDelegate?.dynamicTextViewDidResizeHeight(self, height: height)
}
//constrain Y must be done after setting frame
if updateContentOffsetY != nil {
self.contentOffset.y = updateContentOffsetY!
}
}
func textChanged() {
let caretRect = self.caretRectForPosition(self.selectedTextRange!.start)
let overflow = caretRect.size.height + caretRect.origin.y - (self.contentOffset.y + self.bounds.size.height - self.contentInset.bottom - self.contentInset.top)
if overflow > 0 {
//Fix wrong offset when cursor jumps to next line un explisitly
let seekEndY = self.contentSize.height - self.bounds.size.height
if self.contentOffset.y != seekEndY {
self.contentOffset.y = seekEndY
}
}
}
}
protocol ruuiDynamicTextViewDelegate {
func dynamicTextViewDidResizeHeight(textview: ruuiDynamicTextView, height: CGFloat)
}
How I fix this problem for me:
I have ChatViewController and FooterViewController as UIContainerView. Also, I have contentView outlet in FooterViewController. Then in ChatViewController I have:
override func becomeFirstResponder() -> Bool {
return true
}
override var inputAccessoryView: UIView? {
if let childViewController = childViewControllers.first as? FooterViewController {
childViewController.contentView.removeFromSuperview()
return childViewController.contentView
}
return nil
}
Another way is to create view programmatically and return as inputAccessoryView.
Recently I've wrote a blog post about this exact problem you've described and how to solve it with a short and elegant way by using keyboard notifications but without using the inputAccessoryView. And although this question is pretty old this topic is still relevant so here is the link to the post: Synchronizing rotation animation between the keyboard and the attached view
If you don't want to dive into the long explanation described in the blog post here is a short description with a code example:
The basic principle is to use the same method that everyone uses - observing keyboard notifications to animate the attached view up and down. But in addition to that, you have to cancel these animations when the keyboard notifications are fired as a consequence of interface orientation change.
Rotation example without animation cancellation custom on interface orientation change:
Rotation example with animation cancellation on interface orientation change:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(adjustViewForKeyboardNotification:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(adjustViewForKeyboardNotification:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
self.animatingRotation = YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.animatingRotation = NO;
}
- (void)adjustViewForKeyboardNotification:(NSNotification *)notification {
NSDictionary *notificationInfo = [notification userInfo];
// Get the end frame of the keyboard in screen coordinates.
CGRect finalKeyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// Convert the finalKeyboardFrame to view coordinates to take into account any rotation
// factors applied to the window’s contents as a result of interface orientation changes.
finalKeyboardFrame = [self.view convertRect:finalKeyboardFrame fromView:self.view.window];
// Calculate new position of the commentBar
CGRect commentBarFrame = self.commentBar.frame;
commentBarFrame.origin.y = finalKeyboardFrame.origin.y - commentBarFrame.size.height;
// Update tableView height.
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = commentBarFrame.origin.y;
if (!self.animatingRotation) {
// Get the animation curve and duration
UIViewAnimationCurve animationCurve = (UIViewAnimationCurve) [[notificationInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// Animate view size synchronously with the appearance of the keyboard.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
self.commentBar.frame = commentBarFrame;
self.tableView.frame = tableViewFrame;
[UIView commitAnimations];
} else {
self.commentBar.frame = commentBarFrame;
self.tableView.frame = tableViewFrame;
}
}
Related
i'm working on a project where i have a tableview and a uitextfield.
I'm applying the following method when the uitextfield gain/loose the focus :
-(void)enableInset {
CGFloat offSet = -30.0f;
UIEdgeInsets inset = UIEdgeInsetsMake(placesMapView.frame.size.height - offSet, 0.0f, 0.0f, 00.f);
// Updating the tableView position.
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -(placesMapView.frame.size.height - offSet));
placesTableView.scrollIndicatorInsets = inset;
}
and
- (void)disableInset {
CGFloat offset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
UIEdgeInsets inset = UIEdgeInsetsMake(offset, 0.0f, 0.0f, 00.f);
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -offset);
placesTableView.scrollIndicatorInsets = inset;
}
The enableInset method is called in viewDidLayoutSubviews.
then when i call disableInset and enableInset, the UITableView can not be scrolled anymore.
What did i did wrong ? Any idea where i can look for some answer ?
EDIT :
If it can help, i added the project on github :
https://github.com/Loadex/PlaceViewer
To re-produce the bug :
Scroll the list, tap on the search bar, hit cancel, try to scroll again the list.
Weirdly click on the filter button, when the UIActionSheet is dismissed, the scroll is working again.
While looking for a solution to your problem i noticed that the bottom part of the contentInset of your placesTableView kept changing through the different states. It was 0 when in the initial state where you could see the map, and the tableView was behaving as expected. It got set to 216 when the keyboard came up after tapping the search field. I figured this was some automated communication between the tableView and the keyboard (through Notifications or something you did in PlacesQueryTableViewController). This is fine because we want the bottom inset to be set to the top of the keyboard when it appears. Now, here comes the buggy part. When I tapped the cancel button, the contentInset.bottom got set to -216.
I can't quite explain why this happens, but I suspect it has something to do with how that automatic change of the inset is implemented. I suspect that it does something like tableView.contentInset.bottom -= heightOfKeyboard, and that probably happens when the animation is finished, and not before. The source of your problem is that you change that bottom of contentInset before the animation is done, and thus before that automatic change has happened. So you're setting the bottom to 0 as soon as the user taps cancel. Then the system comes in and reduces it by the height of the keyboard, which turns out to be 216. That's what I think is happening anyway.
To fix this problem, avoid changing the bottom part of the contentInset and just change the top part. placesTableView.contentInset.top is readOnly, but if you do it like in the code below, you can get around that. I have just changed two lines of code in each method, the ones that have to do with the inset. Hopefully you see what I did.
-(void)enableInset {
NSLog(#"Enabling insets");
// Setting the tableView to overlay the map view
CGFloat offSet = [placestableViewController tableView:placestableViewController.tableView heightForRowAtIndexPath:nil] - 30.0f;
UIEdgeInsets inset = placesTableView.contentInset; // UIEdgeInsetsMake(placesMapView.frame.size.height - offSet, 0.0f, 0.0f, 0.0f);
inset.top = placesMapView.frame.size.height - offSet;
// Updating the tableView position.
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -(placesMapView.frame.size.height - offSet));
placesTableView.scrollIndicatorInsets = inset;
placesMapView.hidden = NO;
[placestableViewController loadObjects];}
- (void)disableInset {
NSLog(#"Disable insets");
CGFloat offset = self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height;
UIEdgeInsets inset = placesTableView.contentInset;// UIEdgeInsetsMake(offset, 0.0f, 0.0f, 0.0f);
inset.top = offset;
placesTableView.contentInset = inset;
placesTableView.contentOffset = CGPointMake(0.0f, -offset);
placesTableView.scrollIndicatorInsets = inset;
// Hidding the map while in search
placesMapView.hidden = YES;}
.
BTW, if you want to know how I found the contentInset values at the different states, it's quite simple. What I did was to set myself as the delegate of placesTableView in - (void)viewDidLoad like this placesTableView.delegate = self;. I also had to change the #interfacestatement to #interface KrackMapViewController () <UITableViewDelegate> to say that we conform to the UITableViewDelegate. Now, here's the trick: UITableViewDelegate conforms to UIScrollViewDelegate. That means we can implement methods of the scroll view delegate. The one that is particularly interesting is this one:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(#"Did scroll with insetTop: %f, insetBottom: %f, contentOffset: %f", placesTableView.contentInset.top,
placesTableView.contentInset.bottom,
placesTableView.contentOffset.y);
}
That lets us know when you start dragging the tableView, and in there simply NSLog out the different parameters.
I hope this was helpful. Let me know if this works for you.
After a few research here is what I noticed:
The TableView when the keyboard is released is not scrolling because the tableview seems to believe that it is displayed on the entire screen. I tried to add more data in the tableview and we can see that the view is scrolling a little.
What I believe happened is that when the keyboard is hidden, some automatic calls are done and messing with what I set in my enableInset method. Here is my working solution:
I registered for the hideKeyboard event:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
and in the callback I called enableInset:
- (void)keyboardDidHide: (NSNotification *) notif{
[self enableInset];
}
And the view is scrolling back again.
Any explanation about this are welcome.
I have set a UITextView in the app, and I want that whenever a user types and gets to the keyboard line so the text will bounce one line up so the user can keep typing...
How can I accomplish that? I added the textView in the Interface Builder.
It's fairly complicated.
I wrote up how to do this in a project I have on github called "RandomBlobs" (link).
That project includes working code that uses the technique described.
Here is the article from that project:
Shifting views to make room for the keyboard
When you use an input text field (UITextField or UITextView) in your application, tapping in the text field causes the iOS keyboard to animate up from the bottom of the screen. For a few situations like UITableViewControllers, the system shifts the contents up to make room for the keyboard, but in most cases it does not, and you have deal with this yourself. It's frankly a pain to do this well. You have to allow for fact that the keyboard hight is different for different countries and languages, screen and screen orientations, and can change with OS releases as well. Also Apple can change the timing of the keyboard animation.
Handling this properly involves several steps. The specific details depend on whether your app uses AutoLayout or the older "struts and springs" style resizing rules.
This application uses struts and springs, and shifts the view by altering the view's frame. If you use AutoLayout the details of how you shift the view are slightly different, but the basic idea is the same.
When you receive a UIKeyboardWillShowNotification, it includes a pointer to the NSNotification object. Notification objects include an optional userInfo property that can contain a dictionary with more info about the notification. In the case of keyboard notifications, the userInfo block contains a number of useful key/value pairs, including the frame of the keyboard,in screen coordinates, and the duration of the keyboard show/hide animation. Search on the string "Keyboard Notification User Info Keys" in the Xcode docs for more info on hte user dictionary that is passed to you for keyboard notifications.
Handling keyboard animations requires several steps:
Add observers for 2 different system notifications, UIKeyboardWillShowNotification and UIKeyboardWillHideNotification. If you're writing a single-view application (like the RandomBlobs application) you can add your notification observers in your viewDidLoad:animated method. If you are developing an app with multiple view controllers, though, you probably want to add your observers in your viewWillAppear:animated method. I like to use the new block-based addObserverForName:object:queue:usingBlock: method. You can also use the older `addObserver:selector:name:object:' method, which requires that you have a second method that gets called when the observer gets a notification. Both flavors of observer receive a pointer to the triggering notification object, which is important in handling the keyboard notification.
Add corresponding code to remove your UIKeyboardWillShowNotification and UIKeyboardWillHideNotification observers. For single-view applications, you can do this in your view controlller's dealloc method. For an app with multiple view controllers, you probably want to remove your observers in your viewWillDisappear:animated method.
In order to figure out how far to shift the text field, we need to know it's postion. In order to do that, we need the position of the text field. Sadly, the keyboard notifications don't give us any information about the field that is about to be edited. So, we have to somehow figure out which field is about to begin editing. To do that:
a. Tell the compiler your view controller conforms to the correct protocol (UITextViewDelegate protocol for a UITextView, or UITextFieldDelegate protocol for a UITextField.)
b. Add an instance variable to remember the about-to-be-edited view. (textFieldToEdit in the demo project.)
c. implement the "begin editing" method for your view type (textViewShouldBeginEditing: for a UITextView or textFieldShouldBeginEditing: for a UITextField). The code is simple:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
{
textFieldToEdit = textField;
return YES;
}
d. In IB, set your view controller as the delegate of your UITextView or UITextField.
In the UIKeyboardWillShowNotification code, fetch the keyboard frame and animation duration, as well as the animation curve. These values are provided in both the UIKeyboardWillShowNotification and the UIKeyboardWillHideNotification, but it's usually simpler to just record the information you need into instance variables, which you then use in the handler for the UIKeyboardWillHideNotification handler.
a. Extract the height of the keyboard (from the frame provided in the UIKeyboardFrameBeginUserInfoKey key/value pair) and use it to calculate the amount we need to shift the keybard. We need to figure ot the Y coorindate of the bottom of the input field, in screen coorindates, and then figure out the minimum we need to shift the view up to fully expose the view. (see the code in the demo app, below). In the demo app, we save this value to the instance variable keyboardShiftAmount.
b. Get the animation duration (from the UIKeyboardAnimationDurationUserInfoKey key/value pair) and save it to the float instance variable (called keyboardSlideDuration in the sample app)
c. Save the keyboard animation curve (from the UIKeyboardAnimationCurveUserInfoKey key/value pair) into an instance variable (called keyboardAnimationCurve in the demo project). The keyboard animation curve is a variable of type UIViewAnimationCurve, which ranges from 0 to 4, and is used by the older beginAnimations:context:… commitAnimations style of view animations. We want to use the newer block-based UIView animation method animateWithDuration:delay:options:animations:completion:, which takes animation curve information of enum type UIViewAnimationOptions. The animation curve info in the UIViewAnimationOptions is shifted up by 16 bits, so we have to convert the specified UIViewAnimationCurve to the corresponding UIViewAnimationOptions bit flags by shifting the values by 16 bits (as shown in the code)
Animate the view's frame by the (negative of the) specified keyboard shift amount, and using the duration and animation curve that we got in step 3, above. Some developers only shift the field that's being edited. I think this is confusing, since the field will float up and not longer be at the same position relative to the other fields in the form. Instead, I usually animate the view controller's entire content view up.
In the UIKeyboardWillHideNotification code, do the reverse of the previous step, and animate the view down again. Since we saved the keyboard shift amount, animation duration, and animation curve in the UIKeyboardWillShowNotification handler, this code is pretty simple.
Putting all this togther, let's look at the code from our demo app that adds observers for the UIKeyboardWillShowNotification and UIKeyboardWillHideNotification observers:
showKeyboardNotificaiton = [[NSNotificationCenter defaultCenter] addObserverForName: UIKeyboardWillShowNotification
object: nil
queue: nil
usingBlock: ^(NSNotification *note)
{
//Get the keyboard frame from the notificaiton's userinfo dictionary (in non-rotated screen coordinates)
CGRect keyboardFrame;
NSDictionary* userInfo = note.userInfo;
keyboardSlideDuration = [[userInfo objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];
keyboardFrame = [[userInfo objectForKey: UIKeyboardFrameBeginUserInfoKey] CGRectValue];
keyboardAnimationCurve = [[userInfo objectForKey: UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;
UIInterfaceOrientation theStatusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat keyboardHeight;
//if we're in landscape, treat use the reported keyboard width as the height
if UIInterfaceOrientationIsLandscape(theStatusBarOrientation)
keyboardHeight = keyboardFrame.size.width;
else
keyboardHeight = keyboardFrame.size.height;
CGRect fieldFrame = textFieldToEdit.bounds;
fieldFrame = [self.view convertRect: fieldFrame fromView: textFieldToEdit];
CGRect contentFrame = self.view.frame;
CGFloat fieldBottom = fieldFrame.origin.y + fieldFrame.size.height;
keyboardShiftAmount= 0;
if (contentFrame.size.height - fieldBottom <keyboardHeight)
{
keyboardShiftAmount = keyboardHeight - (contentFrame.size.height - fieldBottom);
//----------------------------------------------------------------------------------------------
//This is the code to shift the view if we're using AutoLayout:
// keyboardConstraint.constant -= keyboardShiftAmount;
// keyboardBottomConstraint.constant += keyboardShiftAmount;
// [self.view layoutIfNeeded];
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//This is the code for handling the keyboard animations for strut-and-spring style view resizing
[UIView animateWithDuration: keyboardSlideDuration
delay: 0
options: keyboardAnimationCurve
animations:
^{
CGRect frame = self.view.frame;
frame.origin.y -= keyboardShiftAmount;
self.view.frame = frame;
}
completion: nil
];
//----------------------------------------------------------------------------------------------
}
}
];
hideKeyboardNotificaiton = [[NSNotificationCenter defaultCenter] addObserverForName: UIKeyboardWillHideNotification
object: nil
queue: nil
usingBlock: ^(NSNotification *note)
{
if (keyboardShiftAmount != 0)
{
//------------------------------------------------------------------------------------------
//This is the code for animating the view back down for strut-and-spring style view resizing
[UIView animateWithDuration: keyboardSlideDuration
delay: 0
options: keyboardAnimationCurve
animations:
^{
CGRect frame = self.view.frame;
frame.origin.y += keyboardShiftAmount;
self.view.frame = frame;
//------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
//This is the code to shift the view back down if we're using AutoLayout
// keyboardConstraint.constant += keyboardShiftAmount;
// keyboardBottomConstraint.constant -= keyboardShiftAmount;
// [self.view setNeedsUpdateConstraints];
// [viewToShift layoutIfNeeded];
//----------------------------------------------------------------------------------------------
}
completion: nil
];
}
}
];
Note that if you're using AutoLayout, there are several more steps and the code is a little different. You need to add a top constraint on your view, with a constant offset from the top layout guide, and a bottom constraint to the view that's tied to the bottom layout guide. Then you need to link thise to IBOutlets in your view controller so you can change their offset amounts in code. In the code above, we've used constraints who's IBOutlets are called keyboardConstraint and keyboardBottomConstraint
Read the Apple documentation on handling the keyboard here.
Listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications and adjust the size of your UITextView accordingly.
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (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 {
CGSize keyboardSize = [[[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] size];
self.textView.frame = CGRectMake(tv.frame.origin.x, tv.frame.origin.y, tv.frame.size.width, self.view.frame.size.height - keyboardSize.height);
}
- (void)keyboardWillHide:(NSNotification *)notification {
self.textView.frame = self.view.bounds;
}
Add an observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillChange:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
then:
- (void)keyboardWillChange:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
CGRect endFrame = [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
endFrame = [self.view convertRect:endFrame fromView:nil];
// yourView.rect = // endFrame...
}
I am a fairly new iPhone developer, and I am working on an iPhone app that has a view where the user needs to enter input into multiple UITextViews. There are a total of 6 UITextViews and when the view appears all the text views are visible without the need to scroll. But when the user clicks on the first text view to enter the text, the last 2 text views become hidden by the keyboard and I can't figure out how to add scrolling capability so the user will be able to scroll when the keyboard is visible. I am using a UIScrollView but currently have no code to make it work since I have tried multiple different things I have found online and none have worked. This may be an easy solution, but I am just out of ideas and have been stuck for a while. Any advice is greatly appreciated. Thanks
More Info: I am using the latest version of Xcode, developing for iPhone versions 6.1 and above. I used the Interface Builder to set up the ScrollView and the AutoLayout box is checked.
in your view did load write following lines
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide) name:UIKeyboardWillHideNotification object:nil];
Make following methods.
-(void)keyboardDidHide
{
scrollview.frame = YOUR_ORIGINAL_FRAME;//You should set frame when keyboard is not there
scrollview.contentSize=scrollview.frame.size;
}
-(void)keyboardDidShow
{
CGRect r = scrollview.frame;
scrollview.contentSize=scrollview.frame.size;
r.size.height - = 216;//216 is keyboard height for iPhone.
scrollview.frame = r;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
/* this returns the keyboard when user click on return button which is reside on keyboard */
if([text isEqualToString:#"\n"])
{
[textView resignFirstResponder];
[yourscrollview setContentOffset:CGPointMake(0,0)animated:YES];
}
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView
{
/* it used for hide keyboard and set all control on their original position */
}
-(void)textViewDidBeginEditing:(UITextView *)textView
{
/* depending upon condition it will scroll the textview so textview can't reside behind the keyboard */
[yourscrollview setContentOffset:CGPointMake(0,textView.center.y-80)animated:YES];
}
above 80 i was defined because my requirement is to take textview that much up when keyboard appears you can put a value which is suitable to your requirements
Follow 2 simple steps as following
From storyboard/xib, adjust the frame for your scrollview and keep
height as screen size.
Within your viewcontroller apply the contentsize for your view like,
<scrollview>.contentSize=CGSizeMake(320, 700);
You will be able to see your entire scrollview on scrolling.
Use following link to move textview or textfield up and down automatically when keyboard appears
https://github.com/michaeltyson/TPKeyboardAvoiding
this link contains demo project. you can use this as you requirement
i hope this will help you.
You could do it this way:
// In View First add keyboard appearance and disappearance Notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
// Now inside this selector method
-(void)keyboardWillShow:(NSNotification *)notify
{
if (!notify) {
return;
}
NSDictionary *userInfo = [notify userInfo];
NSValue *keyboardEndFrame = ([userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]);
CGRect endFrame = keyboardEndFrame.CGRectValue;
// Change the frame of the view to its parent
CGRect loginViewFrame = [loginView.superview convertRect:loginView.frame fromView:loginView];
// Check the keyboard covers the view
if (CGRectGetMinY(endFrame) < CGRectGetMaxY(loginViewFrame))
{
// If YES calculate Distance. Save this difference to animate back the view
difference = CGRectGetMaxY(loginViewFrame)- CGRectGetMinY(endFrame);
// animate that View
[self animateViewUp:YES withMovementDistance:difference];
}
}
// inside Will Hide
-(void)keyboardWillHide
{
// Animate back the view with the calculated distance
[self animateViewUp:NO withMovementDistance:difference];
}
- (void)animateViewUp:(BOOL)up withMovementDistance:(int)movementDistance
{
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView animateWithDuration:movementDuration animations:^{
loginView.frame = CGRectOffset(loginView.frame, 0, movement);
}];
}
I am using content editable html loaded in uiwebview. I need the code to set the cursor position when the keyboard is hidden/shown.
Currently when i click on webview keyboard comes up but the content gets hidden behind the keyboard. Same happens when i keeps pressing on return key the cursor/text goes behind the webview or is not visible.
For head start, i need the functionality like used in iPad Evernote application. In that you can see cursor never goes behind the keyboard, it always starts above the keyboard.
I'm using javascript for this. I'm using a class to keep the code little more organized (so you'll see some this in the code), but that's not necessary.
// this is used to get the current coordinates of the selection - not very efficient, so it shouldn't be called too often
this.updateOffset = function() {
try{
var sel = window.getSelection();
range = sel.getRangeAt(0);
if(this.tmpSpan==null){
this.tmpSpan = document.createElement('span');
}
range.insertNode(this.tmpSpan);
this.yOffset = this.tmpSpan.offsetTop;
this.xOffset = this.tmpSpan.offsetLeft;
this.tmpSpan.parentNode.removeChild(this.tmpSpan);
}
catch(exc){
log('updateOffset:' + exc.toString());
}
}
// eContent is the div with 'contenteditable', while visibleHeight is an int, set from objective-c (depending on where the webview is positioned, keyboard height and screen height)
this.scrollToVisible = function(){
try {
if(this.eContent.clientHeight>this.visibleHeight){
this.updateOffset();
if(this.yOffset<window.pageYOffset){
window.scrollTo(0, this.yOffset);
}
else if(this.yOffset-window.pageYOffset>this.visibleHeight){
window.scrollTo(0, this.yOffset-this.visibleHeight);
}
}
}
catch (exc){
log('scrollToVisible: ', exc.toString());
}
}
In objective-c I'm setting the visibleHeight during keyboard showing up, and afterwards call scrollToVisible when keyboard has finished showing.
-(void)setVisibleHeight:(int)height{
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"docState.visibleHeight=%d", height]];
}
-(void)scrollToVisible{
[self stringByEvaluatingJavaScriptFromString:#"docState.scrollToVisible()"];
}
scrollToVisible is also called on the javascript events: onkeyup, onpaset, oncut, which fixes the issue when pressing 'return' or wrapping on multiple lines.
In case you decide to go this way, you'll need to be very careful when you scroll through javascript, otherwise it may cause some issues with UIWebview control (e.g: placing the cursor at the wrong positions, moving cursor automatically on the top of the document etc.)
Edit
Some clarification regarding the visibleHeight. From what I can remember, I used this because I wasn't able to get the actual visible height from javascript (document.body.clientHeight would also include the area behind the keyboard).
Since I'm presenting the UIWebView in full screen, I'm setting the visible height as follows:
- (void)keyboardWillShow:(NSNotification *)notification {
...
NSDictionary *userInfo = [notification userInfo];
NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
CGRect kbRect = [self.window convertRect:keyboardRect fromView:nil];
_kbRect = kbRect;
CGPoint sorigin = [self.superview convertPoint:self.frame.origin toView:nil];
int visibleHeight = _kbRect.origin.y-sorigin.y-_tlbInputAccessory.frame.size.height-lkPadBottom; // _tlbInputAccessory is a custom accessory view
[self stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"docState.setVisibleHeight(%d)", height]];
...
}
Note that I'm calling this from a subclass of UIWebView, so self will represent the UIWebView control.
Is it possible to get the animation properties (speed and easing curve) for a built in iOS system animation? Specifically, the UIStatusBarAnimationSlide when you hide or show the status bar. Right now I'm just eyeballing it and have come up with a good match: .35 seconds using the default animation curve. This works fine, but Apple is liable to change an animation like this in a future iOS update and it would be nice to match it exactly and not rely on hard-coded values I came up with myself.
For what it's worth, here is the method my view controller is calling when I tap the view to hide the status bar and resize the view to fill the screen.
-(void)tappedView:(UIGestureRecognizer *)gestureRecognizer
{
UIApplication *app = [UIApplication sharedApplication];
// First, toggle the visibility of the status bar
[[UIApplication sharedApplication] setStatusBarHidden:![app isStatusBarHidden] withAnimation:UIStatusBarAnimationSlide];
// Then scale this view controller's view, attempting to match the built-in
// UIStatusBarAnimationSlide animation
[UIView animateWithDuration:.35
animations:^{
self.view.frame = [UIScreen mainScreen].applicationFrame;
}];
}
As an aside, I'm surprised I couldn't find a built in way to handle resizing a VC's view when the status bar is hidden. After all, if the status bar doubles its height when a call is in progress, the view resizes automatically. Tell me I'm missing something and there's a way to get the view to grow automatically, too.
Here's a chuck of code I use in my app:
- (void)application:(UIApplication *)application willChangeStatusBarFrame:
(CGRect)oldStatusBarFrame {
[UIView animateWithDuration:0.355f animations:^{
if(floating_point_values_are_equal(oldStatusBarFrame.size.height, 20.0f)) {
for(UIViewController* VC in self.tabBarController.viewControllers) {
UIView* view = VC.view;
[view setTransform:CGAffineTransformMakeScale(1.0f, 1.0f)];
}
} else {
for(UIViewController* VC in self.tabBarController.viewControllers) {
UIView* view = VC.view;
CGFloat ratio = (view.frame.size.height - 20) / view.frame.size.height;
[view setTransform:CGAffineTransformMakeScale(1.0f, ratio)];
}
}
}];
}
It basically scales the entire app depending on the new screen dimensions. It only works because the scale ratio is not a big change- doing this for the new iPhone screen would not look right.