2013-02-25 40 views
0

在纵向方向上,当键盘出现时我向上推动视图内容,并在返回键按下时将其推下。视图的框架是x = 0,y = 20(放置状态栏) 。在xib中,视图大小设置为自由形式。当更改为横向时,新视图的框架的原点为x = 20 y = 0。理想情况下,它应该是x = 0和y = 20吧?以下是代码。查看框架在方向变化时显示出意外的结果

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 

{ 

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 
    // [self.view setFrame:CGRectMake(0, 20, 320, 460)]; 

} 
else if(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
{ 
    // [self.view setFrame:CGRectMake(0, 20, 480, 300)]; 
} 
    return YES; 

} 

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 

if(toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
{ 

    [lblFirstName setFrame:CGRectMake(100, 134, 84, 21)]; 
    [lblLastName setFrame:CGRectMake(100, 193, 83, 21)]; 
    [txtFirstName setFrame:CGRectMake(273, 129, 142, 31)]; 
    [txtLastName setFrame:CGRectMake(273, 188, 142, 31)]; 
    [btnSubmit setFrame:CGRectMake(194, 243, 93, 37)]; 

} 
else if(toInterfaceOrientation==UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 

    [lblFirstName setFrame:CGRectMake(37, 198, 84, 21)]; 
    [lblLastName setFrame:CGRectMake(37, 282, 83, 21)]; 
    [txtFirstName setFrame:CGRectMake(165, 193, 127, 31)]; 
    [txtLastName setFrame:CGRectMake(165, 277, 127, 31)]; 
    [btnSubmit setFrame:CGRectMake(114, 349, 93, 37)]; 

} 


} 

//Hide keypad on background touch 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
if(self.view.frame.origin.y<0) 
{ 
    [self setViewMovedUp:NO]; 
} 
[self.view endEditing:YES]; 

//[self keyboardWillHide]; 

} 


-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
if ([sender isEqual:txtLastName]) 
{ 
    //move the main view, so that the keyboard does not hide it. 
    NSLog(@"view frame original is %f %f",self.view.frame.origin.x,self.view.frame.origin.y); 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
} 

} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 

[UIView animateWithDuration:0.3 animations:^{ 
    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden comes above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= OFFSET_FOR_KEYBOARD; 
     rect.size.height += OFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     //revert back to the normal state. 
     rect.origin.y += OFFSET_FOR_KEYBOARD; 
     rect.size.height -= OFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 
    NSLog(@"view frame modified is %f %f",self.view.frame.origin.x,self.view.frame.origin.y); 

}]; 

} 

//Return key click handled. 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
if(self.view.frame.origin.y<0) 
{ 
    [self setViewMovedUp:NO]; 
} 

[textField resignFirstResponder]; 

return YES; 

} 
+0

根据您XIB文件限制等view.bounds,框架可以willRotoateToInterfaceOrientation后应用更改。请尝试使用didRotateFromInterfaceOrientation。 – 2013-02-25 11:48:16

+0

什么样的约束?你能否指教一下。 – Raj 2013-02-25 11:50:19

+0

您是否尝试使用didRotateFromInterfaceOrientation?我指的是可以在界面构建器中定义的布局约束,以及当您不太在意的时候自动定义自己的布局约束。 – 2013-02-25 12:29:36

回答

0

我在iOS7中遇到了同样的问题。 同时,相同的代码在iOS8中正常工作。

要解决这个问题,你需要转移,而不是view.frame