在编辑时移动文本

时间:2011-11-07 09:29:25

标签: xcode

我的应用中的视图大致如下:

一个UIImage,除此之外,还有一个描述图像的TextView。 - 文本应该是可编辑的。

现在这是我的问题:当用户点击文本字段时,外观键盘"位于文本" (用户无法看到,他在写什么)。 是否有一个易于实现的可能性(我是XCode的新手)将文本切换到页面的上半部分,同时编辑它(类似于:"如果键盘上的内容替换图像的文字" ,"如果键盘消失撤消"), 这样用户可以看到变化吗?

感谢您的帮助,   最大

3 个答案:

答案 0 :(得分:1)

首先,您正在使用文本字段或文本视图,我认为您正在使用文本字段

// #define kOFFSET_FOR_KEYBOARD 110.0在.m文件中定义它并相应地设置其值

-(void)textFieldDidBeginEditing:(UITextField *)textField{      
    if(self.view.frame.origin.y >= 0)
        [self setViewMoveUp:YES];
}
}


-(void)setViewMoveUp:(BOOL)moveUp{

CGRect rect = self.view.frame;
if(moveUp)
{
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    rect.size.height -= kOFFSET_FOR_KEYBOARD;

}
self.view.frame = rect;

}

答案 1 :(得分:0)

这样做。当委托方法调用时(即在beginEditing方法中)

编写此代码

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
[self.view setFrame:CGRectMake(0,-20,320,400)];
[UIView commitAnimations];

答案 2 :(得分:0)

- (void)textFieldDidBeginEditing:(UITextField *)textField 

{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
txtField.frame = CGRectMake(txtField.frame.origin.x, (txtField.frame.origin.y), txtField.frame.size.width,txtField.frame.size.height);
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];

UIInterfaceOrientation des=self.interfaceOrientation;
if (des==UIInterfaceOrientationLandscapeLeft||des==UIInterfaceOrientationLandscapeRight)
{
txtField.frame=CGRectMake(500,250,97,37);
}
[UIView commitAnimations];
}

}

" Don't forget to given this in Vied DidLoad"

self.view.frame=[[UIScreen mainScreen]applicationFrame];
view.autoresizesSubviews=NO;
txtField.delegate=self;