我知道这是一个常见问题,但我没有运气从我发现的示例中实现这个问题。我可能做了一些简单的错误......(希望如此)。我正在尝试将 UIview 向上移动,因为底部隐藏了一个文本字段。
关于我的应用程序的一些信息,它有一个选项卡栏 Controller 并实现了标准 UIView。当我添加代码以向上移动显示时,什么也没有发生。我是否需要有一个 ScrollView 才能工作,是否与选项卡 Controller 冲突或者我做错了什么?谢谢
丹
最佳答案
我在 UIView 上写了一个小类别,它可以管理临时滚动的内容,而不需要将整个内容包装到 UIScrollView 中。我在这里使用动词“滚动”可能并不理想,因为它可能会让您认为涉及 ScrollView ,但事实并非如此——我们只是对 UIView(或 UIView 子类)的位置进行动画处理。
其中嵌入了一堆适合我的形式和布局的神奇数字,但可能不适合您的形式和布局,因此我鼓励对其进行调整以满足您的特定需求。
UIView+FormScroll.h:
#import <Foundation/Foundation.h>
@interface UIView (FormScroll)
-(void)scrollToY:(float)y;
-(void)scrollToView:(UIView *)view;
-(void)scrollElement:(UIView *)view toPoint:(float)y;
@end
UIView+FormScroll.m:
#import "UIView+FormScroll.h"
@implementation UIView (FormScroll)
-(void)scrollToY:(float)y
{
[UIView beginAnimations:@"registerScroll" context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.4];
self.transform = CGAffineTransformMakeTranslation(0, y);
[UIView commitAnimations];
}
-(void)scrollToView:(UIView *)view
{
CGRect theFrame = view.frame;
float y = theFrame.origin.y - 15;
y -= (y/1.7);
[self scrollToY:-y];
}
-(void)scrollElement:(UIView *)view toPoint:(float)y
{
CGRect theFrame = view.frame;
float orig_y = theFrame.origin.y;
float diff = y - orig_y;
if (diff < 0) {
[self scrollToY:diff];
}
else {
[self scrollToY:0];
}
}
@end
将其导入到你的 UIViewController 中,然后你就可以这样做
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.view scrollToView:textField];
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
[self.view scrollToY:0];
[textField resignFirstResponder];
}
...或者其他什么。该类别为您提供了三种非常好的方法来调整 View 的位置。
关于iphone - 当键盘显示时向上移动 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5220818/