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;
}
의 view.bounds을 이동하기 위해 필요한이 문제를 해결하려면 프레임 워크는 willRotoateToInterfaceOrientation 후 변경 사항을 적용 할 수 있습니다. 대신 didRotateFromInterfaceOrientation을 사용해보십시오. –
어떤 종류의 제약 조건입니까? 너는 그걸 깨달을 수 있겠 니? – Raj
대신 didRotateFromInterfaceOrientation을 사용해 보셨습니까? 필자는 인터페이스 빌더에서 정의 할 수있는 레이아웃 제약 조건을 언급하고 있었고 그다지 신경 쓸 필요가 없다면 자동으로 그 정의를 정의했습니다. –