I followed the Apple documentation to move a textfield upwards when the keypad appears.
The code works fine my problem is that I need that one specific textfield is moved towards the other, instead of implementing the code Apple every textfield I select is moved upwards ... How can I do to move a specific textField and not all?
Thank you very much, I insert the following code used
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = changePasswordTextField.superview.frame;
bkgndRect.size.height -= kbSize.height;
[scrollView setContentOffset:CGPointMake(0.0, changePasswordTextField.frame.origin.y+kbSize.height) animated:YES];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
[scrollView setContentOffset:CGPointZero animated:YES];
}
You can achieve your functionality by following steps.
Set delegate of your UITextField.
Implement textFieldDidBeginEditing method which will be called when keyboard open for textfield. So you may change frame of textfield in this method as below.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
// VALUE = textfield you want to move upward vertically
}
Now, to handle keyboard hiding event, you can set frame of your textfield to its origin in textFieldDidEndEditing method as below.
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
// VALUE = textfield you want to move downward vertically
}
I hope it may help you.
Related
After switching to iOS8, I'm getting weird behavior when I move views during a keyboard transition. Can anyone explain what's going on?
Here's a minimal example to demonstrate the problem. I have a simple view with a UITextField and a UIButton. The function nudgeUp moves the text field and the button up by 10 points. It is triggered either by the buttonPressed callback, or the keyboardWillShow callback.
When I tap the button, the code works as expected: buttonPressed calls nudgeUp and the button and text field jump up by 10 points.
When I tap the text field, keyboardWillShow calls nudgeUp, but the behaviour is very different. The button and text field immediately jump down by 10 points, and then slide back up to their original position as the keyboard shows itself.
Why is this happening? How can I regain control of animations during keyboard presentation in iOS8?
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Called when the keyboard appears.
[self nudgeUp];
}
- (IBAction)buttonPressed:(id)sender {
[self nudgeUp];
}
- (void)nudgeUp
{
CGRect newTextFieldFrame = self.textField.frame;
newTextFieldFrame.origin.y -= 10;
self.textField.frame = newTextFieldFrame;
CGRect newButtonFrame = self.button.frame;
newButtonFrame.origin.y -= 10;
self.button.frame = newButtonFrame;
}
#end
It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change can be animated.
Example:
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.bottomSpaceConstraint.constant = adjustmentedValue;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
You should use UIKeyboardDidShowNotification (you're using will version) and everything will work as you expect:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Called when the keyboard finished showing up
[self nudgeUp];
}
The explanation is that with UIKeyboardWillShowNotification you are changing the frames too early. After your changes the system will relayout everything to accomodate the keyboard and your changes won't have any effect.
Also, I recommend you to switch to autolayout and forget about frames.
Try using the UIKeyboardWillShowNotification userInfo to give you the frame of the keyboard. Then move the onscreen elements based on that.
I have a TabBarController, supportedInterfaceOrientations - UIInterfaceOrientationMaskPortrait. Navigation bar moves up under the status bar when I start watching video from one of tabs, rotate device to landscape and then exit from fullscreen. Why it's happens and how fix it?
This is a kind of hack, but works in my view controller contains UIWebView and fullscreen videos start from <video> tags.
You will see jerking of navigation bar because UIWindowDidBecomeHiddenNotification is called after video fullscreen window just disappear.
- (void)viewDidLoad
{
[super viewDidLoad];
// NOTE: I'm not sure, but MPMoviePlayerWillExitFullscreenNotification won't work
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)windowDidBecomeHidden:(NSNotification *)notification
{
UIView *navbar = self.navigationController.navigationBar;
CGRect barFrame = navbar.frame;
barFrame.origin.y = 20;
self.navigationController.navigationBar.frame = barFrame;
UIView *navbarBack = nil;
for (UIView *view in navbar.subviews) {
if ([NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"]) {
navbarBack = view;
break;
}
}
CGRect backFrame = navbarBack.frame;
backFrame.origin.y = -20;
backFrame.size.height = 64;
navbarBack.frame = backFrame;
[navbar.superview setNeedsLayout];
}
UIView *navbarBack = [navbar.subviews bk_match:^BOOL(UIView *view) {
return [NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"];
}];
I have met the same issue.I tried to use js to get the "webkitendfullscreen" event,and then found navbar.frameOriginY = 0,which should be 20.
How to changed the upper keyboard bar color and button color?
I set the textfield delegate .
Then the keyboard show the bar, I need change the bar color and button.
But I don't know how to set.
Have any one can help me? thank you very much.
1.Add A Notification for Keyboard to no if the keyboard is will (did) show
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(liftMainViewWhenKeybordAppears:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
2.Then in liftMainViewWhenKeybordAppears: method, You can change the Bar Color and Buton.
- (void)liftMainViewWhenKeybordAppears:(NSNotification *)notice{
yourBar.backgroundColor = [UIColor redColor];
[yourButton setTitle:#"newTitle" forState:UIControlStateNormal];
}
3.Remove Notification
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
I was wondering how to swipe the ViewController with a visible keyboard?
in iOS 7 I can swipe the ViewController from side to side, but the keyboard stays put.
in short, I would like to get to the following state:
Thanks!
Update:
I can't recommend the original solution. While it performed well (when it performed at all), it was an unreliable hack, and could easily break the pop gesture recognizer.
My colleague Dave Lyon came up with a great solution using iOS 7 view controller transitions and packaged it up into a pod:
https://github.com/cotap/TAPKeyboardPop
Once installed, just import the main file and you should be good to go.
Original:
I'd love to know if there's a better way of doing this, but I was able to achieve the behavior by adding the keyboard's view as a subview of the view controller's main view:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.inputAccessoryView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillHide:(NSNotification *)note
{
if (self.textView.isFirstResponder) {
UIView *keyboardView = self.textView.inputAccessoryView.superview;
if (keyboardView) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:keyboardView];
});
}
}
}
I've found you can also animate the keyboard with the gesture (via addTarget:action:), but the performance is abysmal and doesn't cleanly animate if the gesture is prematurely canceled.
I am using a Master-Detail template.
I have a segmented control in the detail view and I have set the MasterViewController as the delegate.
This enables me to give the user choices.
I know the segmented control is working and passing the choices to the MVC.
I want each choice to trigger a new set of data which can then be loaded into the table view in the MasterViewController.
My problem is that I cannot then find a way to update the data in the table view.
You can use an instance variable declared in MasterViewController to be updated while UISegmentedControl changes. Based on that, When you come back to the MasterViewController; use its viewWillAppear to [tableView reloadData]
Tell me if I am getting your question wrong.
EDIT:
Take an integer name it segmentIndex; update as and when Segment gets changed. Based on the value of segmentIndex load the dataSource needed to be displayed on UITableView of your MasterViewController
EDIT 2:
When UISegmentedControl's value changes, put mvc.segmentIndex = (currentValueOfYourSegmentedControl);
Then in MasterViewController's viewWillAppear
switch (self.segmentIndex)
{
case 0:
// Set Datasource for First Choice.
break;
case 0:
// Set Datasource for Second Choice.
break;
case 0:
// Set Datasource for Third Choice.... and so on...
break;
default:
// Default Behavior
break;
}
Hope You Get My Point.
If it was me I would use notifications instead of delegates. i.e Detect the segmentedControl value change using valueChanged inside the detailViewController and then post an NSNotification from your detailViewcontroller which your masterViewController receives.
Easiest way is:
[[NSNotificationCenter defaultCenter] postNotificationName:#"segmentOneChosen" object:nil userInfo:nil];
Have you masterViewController register for this notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(segmentOneChosen) name:#"segmentOneChosen" object:nil];
It would be better still to pass the chosen value along with the notification so you don't need a separate notification for each segment.
!! untested code:
NSArray *keys = [NSArray arrayWithObjects:#"segmentChosen", nil];
NSArray *objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:self.topicsChoiceSegControl.selectedSegmentIndex], nil];
NSDictionary * dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"segmentChosen" object:nil userInfo:dict];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(segmentChosen:) name:#"segmentChosen" object:nil];
-(void) segmentChosen:(NSNotification *)notification {
NSNumber *segmentChosenNum = [[notification userInfo] valueForKey:#"segmentChosen"];
}
I know this doesn't answer your question per se, but it does provide an alternative solution to the problem you are trying to overcome.