以下是 portrait 文本字段移动的一些很棒的教程。
另一方面,我的视图旋转为纵向和横向,并且在两个方向上键盘都遮挡了文本字段......现在,纵向和一个的横向方向都可以工作。
所以我想知道如何将两种景观方向都包括在内。
这就是我正在做的事情:
-(void) keyboardWillShow:(NSNotification *)notif{
if ([serverIP isFirstResponder]){
if (isPortrait && self.view.frame.origin.y >= 0){
[self setViewMovedVertical:YES];
}
else if (!isPortrait && self.view.frame.origin.x >= 0){
[self setViewMovedHorizontal:YES];
}
}
}
移动视图。以下是相应的方法
#define PORTRAIT_KEY_OFF 216
#define LANDSCAPE_KEY_OFF 140
-(void) setViewMovedVertical:(BOOL)movedUp{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (movedUp){
rect.origin.y -= PORTRAIT_KEY_OFF;
rect.size.height += PORTRAIT_KEY_OFF;
}
else{
rect.origin.y += PORTRAIT_KEY_OFF;
rect.size.height -= PORTRAIT_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void) setViewMovedHorizontal:(BOOL)moved{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (moved){
rect.origin.x -= LANDSCAPE_KEY_OFF;
rect.size.width += LANDSCAPE_KEY_OFF;
}
else{
rect.origin.x += LANDSCAPE_KEY_OFF;
rect.size.width -= LANDSCAPE_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
以下是将其移回的方法
-(IBAction) serverIPDone: (UITextField *) sender{
if ([serverIP isFirstResponder]){
if (self.view.frame.origin.y < 0){
[self setViewMovedVertical:NO];
}
if (self.view.frame.origin.x < 0){
[self setViewMovedHorizontal:NO];
}
[serverIP resignFirstResponder];
}
}
希望你能帮忙!如果我反消歧(看我在那里做了什么?)这个问题,请告诉我!
答案 0 :(得分:2)
最终想出来!!!!
这是错误的:原点在两种景观模式之间确实发生了变化。所以你要做的就是检测你所处的景观版本,并根据它添加或减去!
-(IBAction) serverIPDone: (UITextField *) sender{
if ([serverIP isFirstResponder]){
if (self.view.frame.origin.y < 0){
[self setViewMovedVertical:NO];
}
if (self.view.frame.origin.x != 0){
[self setViewMovedHorizontal:NO];
}
[serverIP resignFirstResponder];
}
}
上一个确保键盘未设置。
-(void) setViewMovedVertical:(BOOL)movedUp{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (movedUp){
rect.origin.y -= PORTRAIT_KEY_OFF;
rect.size.height += PORTRAIT_KEY_OFF;
}
else{
rect.origin.y += PORTRAIT_KEY_OFF;
rect.size.height -= PORTRAIT_KEY_OFF;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void) setViewMovedHorizontal:(BOOL)moved{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
CGRect rect = self.view.frame;
if (moved){
if (leftLandscape)
rect.origin.x -= LANDSCAPE_KEY_OFF;
else
rect.origin.x += LANDSCAPE_KEY_OFF;
}
else{
if (leftLandscape)
rect.origin.x += LANDSCAPE_KEY_OFF;
else
rect.origin.x -= LANDSCAPE_KEY_OFF;
NSLog(@"after: %f",rect.origin.x);
}
self.view.frame = rect;
[UIView commitAnimations];
}
上一页将执行键盘的实际移动。我摆脱了调整大小,你可以把它重新加入。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)x
duration:(NSTimeInterval)duration{
if (UIInterfaceOrientationIsPortrait(x)) {
isPortrait = TRUE;
}
else {
isPortrait = FALSE;
leftLandscape = (x == UIInterfaceOrientationLandscapeLeft);
}
}
上一页会确保您正确设置变量isPortrait和leftLandscape。
花了太长时间,但我终于 DONE !!!