show the keyboard of a textfield on top of the screen - iphone

In my iPhone apps, my problem is that I have a textfield at the bottom of the screen, so when the keyboard appear, he hides the textfied, there is a way to show the keyboard on the top of the screen?

You should move your view when the keyboard appears.
The code is:
In .m file
- (void) loginViewUp : (UIView*) view
{
if(!alreadyViewUp)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = view.frame;
rect.origin.y -= View_Move_Hight;
view.frame = rect;
[UIView commitAnimations];
alreadyViewUp = !alreadyViewUp;
}
}
- (void) loginViewDown :(UIView*) view
{
if(alreadyViewUp)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = view.frame;
rect.origin.y += View_Move_Hight;
view.frame = rect;
[UIView commitAnimations];
alreadyViewUp = !alreadyViewUp;
}
}
In .h file
- (void) loginViewUp : (UIView*) view;
here
#define View_Move_Hight 170
is defined before #implementation.

You should design your view so that it shifts up with the keyboard, iPhone users are used to the keyboard always being on the bottom of the screen so this would go against the HIG

Related

View animation frame change

Are emerging in the subview scrolling animation. However actionsheet place when you scroll subview, not again appear in the first place.
if(checkboxState == 0)
{
NSTimeInterval animationDuration = 0.8;
CGRect newFrameSize = CGRectMake(0, 155,320,120);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
subview.frame = newFrameSize;
[UIView commitAnimations];
}
else if(checkboxState == 1)
{
NSTimeInterval animationDuration = 0.8;
CGRect newFrameSize = CGRectMake(0, 105,320,120);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
subview.frame = newFrameSize;
[UIView commitAnimations];
}
Pressing the View is a checkbox in the above code is provided to move up and down. However, even though actionsheet slipped up again when the first view is in place.
View is scrolled up, how can we keep it that way, even if opened actionsheet?
Use [UIView setAnimationBeginsFromCurrentState:YES]; after both "beginAnimation".
Then the previous changes will be saved.
I fixed the problem by using the
subview.bounds
Thank you for your helps.

Keep UIScrollView in correct place after switching from landscape to portrait

I have a UIScrollView that contains a text field and a text view. I have code to move the text fields up when the keyboard is present so the keyboard does not cover the text fields. This code works great in portrait view:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField) {
[textField resignFirstResponder];
}
return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewDidBeginEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewShouldEndEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
When I rotate the iPhone simulator into a landscape view, this (not surprisingly) does not work. How can I get the text field to move up just enough to see it while typing into it in both landscape and portrait views?
Also, if I rotate from landscape to portrait while the keyboard is shown, after dismissing the keyboard the scroll view has moved down the screen instead of lined up in its original position. How can I avoid this?
Apple's document Managing the Keyboard shows the proper way to do this under the heading "Moving Content That Is Located Under the Keyboard".
You basically put your content on a scroll view and use the UIKeyboardDidShowNotification and UIKeyboardWillHideNotification notifications to adjust the content offsets.
However, the code is somewhat incomplete. The keyboard size calculation doesn't work in landscape mode. To fix it, replace this:
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
With this:
// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];
CGSize kbSize = kbRect.size;

UITableView resizing when moving the container UIView

I have an application where I'm moving my entire UIView up when using the keyboard, in order to not hide some important UITextFields.
My problem is that the UITableView that I have there does not behave the same way as all the other views. Instead of moving the entire UITableView, it moves only its top edge, and that resizes the UITableView which is not the desired effect.
My problem is better described here: http://www.youtube.com/watch?v=AVJVMiBULEQ
This is the code that I'm using for the animation:
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
CGRect rect = self.view.frame;
if (movedUp) {
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= 140;
rect.size.height += 140;
} else {
// revert back to the normal state.
rect.origin.y += 140;
rect.size.height -= 140;
}
self.view.frame = rect;
[UIView commitAnimations];
}
As solved in comments above, code above works when view is not autoresizing. The simpler approach would be to use CGAffineTransform transformations that calculate view frame instead of you.
-(void)setViewMovedUp:(BOOL)movedUp {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view
[UIView setAnimationBeginsFromCurrentState:YES];
if (movedUp) {
self.transform = CGAffineTransformMakeTranslation(0, -150);//move up for 150 px
} else {
self.transform = CGAffineTransformIdentity;//reset to initial frame
}
[UIView commitAnimations];
}

Swiping A View with Gestures

I am trying to develop an application to Iphone; but i am new-bee so i have got a lot of problems.
My application has got a default view and another small view in the main view that name is flashcard.
I want to add capability this view swiping (or sliding) like Photos in Iphone. For example if user swipes view until the inner view's center reaches bounds of main view there will be two possibilities,
1- if user finish swiping before reach inner view will return to original position.
2- if user don't stop swiping, inner view will go out at screen from swipe direction and return the screen from the opposite direction.
So i declare UIPanGestureRecognizer at viewDidLoad like below
UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanFlashCard:)];
[flashcard addGestureRecognizer:panFlashCard];
[panFlashCard release];
handPanFlashCard action:
UIView *piece = [recognizer view];
CGPoint center = piece.center;
CGFloat x = 160; // the original x axis of inner view
if ([recognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:piece ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)];
[UIView commitAnimations];
CGFloat totalX;
totalX= x + translation.x;
if(translation.x <0)
{
if (totalX <= 0)
{
[self showNextCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
else
{
if (totalX >= self.view.bounds.size.width)
{
[self showPreviousCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
}
showNextCard action:
[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
showPreviousCard action:
[flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
When i run this application, it builds and runs successfully but there is something wrong with animations, some frame mistakes i think.
Please help me to correct this code and run fluent animations while swiping.
If I understand what you are trying to do then you can just use built in controls for this. Take a look at the pagingEnabled property on the UIScrollView class. There is also a sample project called PhotoScroller that may help.
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html

How can I present a UIView from the bottom of the screen like a UIActionSheet?

I'd like a UIView to slide up from the bottom of the screen (and stay mid-screen) like a UIActionSheet. How can I accomplish this?
UPDATE:
I am using the following code:
TestView* test = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
test.view.center = CGPointMake(160,100);
//test.view.frame = CGRectMake(0, 0, 160, 210);
[[[UIApplication sharedApplication] keyWindow] addSubview:test.view];
[UIView commitAnimations];
The view seems to be animating from the corner and appearing in the corner. How can I make it slide up from the bottom? Getting close!
Do what Matt did here, but just change the values and direction. I have code at home to do this from the bottom if you need it later (I'll update this post).
Link: http://cocoawithlove.com/2009/05/intercepting-status-bar-touches-on.html
Also, don't forget to take out the bit of code that shifts the main view downward (so instead the UIView just pops over top like an ActionSheet)
Updated with code:
This is what I use in one of my apps to show/hide a little "options" view:
- (void)toggleOptions:(BOOL)ViewHidden
{
// this method opens/closes the player options view (which sets repeat interval, repeat & delay on/off)
if (ViewHidden == NO)
{
// delay and move view out of superview
CGRect optionsFrame = optionsController.view.frame;
[UIView beginAnimations:nil context:nil];
optionsFrame.origin.y += optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[UIView commitAnimations];
[optionsController.view
performSelector:#selector(removeFromSuperview)
withObject:nil
afterDelay:0.5];
[optionsController
performSelector:#selector(release)
withObject:nil
afterDelay:0.5];
optionsController = nil;
}
else
{
optionsController = [[PlayOptionsViewController alloc] init];
//
// Position the options at bottom of screen
//
CGRect optionsFrame = optionsController.view.frame;
optionsFrame.origin.x = 0;
optionsFrame.size.width = 320;
optionsFrame.origin.y = 423;
//
// For the animation, move the view up by its own height.
//
optionsFrame.origin.y += optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[window addSubview:optionsController.view];
[UIView beginAnimations:nil context:nil];
optionsFrame.origin.y -= optionsFrame.size.height;
optionsController.view.frame = optionsFrame;
[UIView commitAnimations];
}
}
One way would be to use the present modal view controller on the view controller:
presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
For more info take a look at the UIViewController documentation.
EDIT: If you want a mid-screen view you'll need to animate it into position as #jtbandes has pointed out. I suggest also adding some candy to UIView animation block:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
myView.center = CGPointMake(x,y);
[UIView commitAnimations];
You can then move it again if you need to go full screen or dismiss it.
You'll have to move the view yourself, by setting its center or frame. I'll let you figure out what to set those to. But for the animation:
// set the view to its initial position here...
[UIView beginAnimations:nil context:NULL];
// move the view into place here...
[UIView commitAnimations];
Check out this post: http://blog.yetanotherjosh.com/post/33685102199/3-ways-to-do-a-vertical-transition-with
I'm going with the modal window approach.
Try this solution.... it works
#pragma mark - Date Selector View PresentModelView with Transparent ViewController
- (void) showModal:(UIView*) modalView {
CGRect rect=modalView.frame;
rect.origin=CGPointMake(0, 0);
self.tutorialView.frame=rect;
UIWindow *mainWindow = [(AppDelegate *)[UIApplication sharedApplication].delegate window];
CGPoint middleCenter;
middleCenter = CGPointMake(modalView.center.x, modalView.center.y);
CGSize offSize = [UIScreen mainScreen].bounds.size;
CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
modalView.center = offScreenCenter;
if ([[mainWindow subviews] containsObject:modalView]) {
[modalView removeFromSuperview];
}
[mainWindow addSubview:modalView];
[mainWindow bringSubviewToFront:modalView];
// Show it with a transition effect
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
// animation duration in seconds
modalView.center = middleCenter;
[UIView commitAnimations];
}
// Use this to slide the semi-modal view back down.
- (void) hideModal:(UIView*) modalView {
CGSize offSize = [UIScreen mainScreen].bounds.size;
CGPoint offScreenCenter = CGPointMake(offSize.width / 2.0, offSize.height * 1.5);
[UIView beginAnimations:nil context:(__bridge void *)(modalView)];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(hideModalEnded:finished:context:)];
modalView.center = offScreenCenter;
[UIView commitAnimations];
}
- (void) hideModalEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIView *modalView = (__bridge UIView *)context;
[modalView removeFromSuperview];
}