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;

}
4

1 回答 1

0

我在iOS7中遇到了同样的问题。同时,相同的代码在 iOS8 中运行良好。

要解决此问题,您需要移动 view.bounds 而不是 view.frame

于 2015-04-27T03:19:16.237 回答