2013-10-20 2 views
-1

가로 및 세로 방향을 다르게 배치해야하는보기 컨트롤러가 있습니다. 다음 코드는 하나의 작은 것을 제외하면 거의 완벽하게 작동합니다. 이동 된 버튼 (태그가 3 인 버튼)의 경우, 다른 버튼 (태그 = 0 및 호출 (IBAction) myButton :)을 클릭하면 단추가 놓인 위치로 이동합니다 스토리 보드에. 레이블은 움직이지 않고 단추 만 움직입니다. 이는 (IBAction) myButton에 설정된 제목의 값이 버튼을 누르기 전의 값과 다른 경우에만 발생합니다.버튼이 예상대로 움직이지 않는 이유

- (IBAction)myButton:(UIButton *)sender { 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setTitle: @"A" forState:UIControlStateNormal]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [email protected]"B"; 
} 
-(void)rotateViewToPortrait{ 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setFrame:CGRectMake(100, 500, but2.frame.size.width, but2.frame.size.height)]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [lab setFrame:CGRectMake(100,550 , lab.frame.size.width, lab.frame.size.height)]; 
} 
-(void)rotateViewToLandscape{ 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setFrame:CGRectMake(500, 100 , but2.frame.size.width, but2.frame.size.height)]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [lab setFrame:CGRectMake(500,150 , lab.frame.size.width, lab.frame.size.height)]; 
} 
-(void)viewDidAppear:(BOOL)animated{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft)) { 
     [self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationLandscapeRight duration:0]; 
    } else { 
     [self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0]; 
    } 
} 
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)||(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)) { 
     [self rotateViewToLandscape]; 
    } else if ((toInterfaceOrientation == UIInterfaceOrientationPortrait)||(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){ 
    [self rotateViewToPortrait]; 
    } 
} 

답변

0

스토리 보드의보기 컨트롤러에서 AutoLayout을 해제하여 문제를 해결했습니다. 또한 모든 스프링과 스트럿을 제거하고 기본 뷰에서 자동 크기 조정 하위 뷰를 사용하지 않도록 설정했습니다. 나는 왜 동작이 위와 같이 설명되었는지 아직 확실하지 않습니다. 그러나 헤이 호 그것은 지금 일한다.

+0

Upvoted-이 문제는 즉시 해결되었습니다. 나는 어떤 제약도 사용하지 않고 있지만 xib는 "Use Auto Layout"을 체크했고 서브 뷰의 위치는 예기치 않은 방식으로 상쇄되었습니다. – bmovement

0

나는 비슷한 문제가있었습니다.

이 코드 :

if (howManyButtons==2) { 
    button1.hidden=true; 
    button2.hidden=false; 
    [button2 setFrame:CGRectMake(177,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button3.hidden=false; 
    [button3 setFrame:CGRectMake(703,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button4.hidden=true; 
} else { 
    button1.hidden=false; 
    [button1 setFrame:CGRectMake(71,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button2.hidden=false; 
    [button2 setFrame:CGRectMake(317,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button3.hidden=false; 
    [button3 setFrame:CGRectMake(563,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button4.hidden=false; 
    [button4 setFrame:CGRectMake(809,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
} 

때 버튼의 이미지가 다음과 같이 코드로로드 된 경우, (2 == howManyButtons) 예상

UIImage *btnImage4 = [UIImage imageWithContentsOfFile:path]; 
    [button4 setImage:btnImage4 forState:UIControlStateNormal]; 

단추가 이동하지만 버튼의 이미지에이 코드가로드되었을 때 (앱 자체의 리소스 대신 iPad의 포토 라이브러리에서 이미지를 가져 오는 것) :

버튼이 움직이지 않았으므로 Main.storyboard에있는 위치에 머물렀습니다.

내가 행동의 차이를 추측하고있어

는 자동 레이아웃은 뽀빠이처럼 문제를 해결하지만 끄기 ..... 버튼 이미지를 설정하는 자산 라이브러리 코드가 비동기 적으로 실행된다는 사실까지입니다 나는 왜 (나는 싫어하는지) 이해하지 못한다.

관련 문제