在纵向方向上,我在键盘出现时向上推视图内容,按下返回键时将其向下按。视图的框架为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 :(得分:0)
我在iOS7中遇到了同样的问题。 同时,相同的代码在iOS8中运行良好。
要解决此问题,您需要转换view.bounds而不是view.frame