2013-02-27 1 views
0

저는 5 개의 UIButton을 가지고 있으며, Landscapeportrait 크기가 서로 다를 수 있도록 UIButton 프레임을 설정해야합니다. 하지만 아래의 코드 viewDidLoad로드가 잘되지만 문제는 스택을로드 한 후 에서 portrait 또는 portrait에서 Landscape으로 이동하면 설정 한 viewdidload 크기에 따라 변경해야합니다. 일어나지 않는 ... 이전 버튼이 이미 생성 된 것처럼 오버랩되었습니다 .... 어떤 변화가 필요합니까? 뷰를 변경하면 .. 버튼이 제대로 나타나야합니다. 겹치지 않아야합니다.LandScape/Portrait 크기가 다릅니다. iOS

-(void)viewDidLoad{ 
int Height = 200; 
    int Yposition = 20; 
    for (int k=0; k<5; k++) 
    { 
if ([UIApplication sharedApplication].statusBarOrientation== UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) { 

     UIButton *loPlayButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [loPlayButton addTarget:self action:@selector(PlayButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
      // loPlayButton.tag = 100+k; 
     NSLog(@"tag is %i",loPlayButton.tag); 
     loPlayButton.alpha = 1.0; 

     UIImage *image=[UIImage imageNamed:@"vid.png"]; 
     [loPlayButton setBackgroundImage:image forState:UIControlStateNormal]; 
     loPlayButton.frame=CGRectMake(520, Yposition +(k*Height +170), image.size.width, 30); 
     // loPlayButton.tag=3; 


     [mScrollView_por addSubview:loPlayButton]; 
     [mPlayButtonsArray addObject:loPlayButton]; 
      } 



     if ([UIDevice currentDevice].orientation==UIInterfaceOrientationPortrait || [UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown) { 


      UIButton *loPlayButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [loPlayButton addTarget:self action:@selector(PlayButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
       // loPlayButton.tag = 100+k; 
      NSLog(@"tag is %i",loPlayButton.tag); 
      loPlayButton.alpha = 1.0; 

      UIImage *image=[UIImage imageNamed:@"vid.png"]; 
      [loPlayButton setBackgroundImage:image forState:UIControlStateNormal]; 
      loPlayButton.frame=CGRectMake(440, Yposition +(k*Height +170), image.size.width, 30); 
       // loPlayButton.tag=3; 


      [mScrollView_por addSubview:loPlayButton]; 
      [mPlayButtonsArray addObject:loPlayButton]; 

     }}} 


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

[self viewDidLoad]; 
} 

답변

1
- (void)viewWillAppear:(BOOL)animated 
{ 
    UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if(UIInterfaceOrientationIsPortrait(statusBarOrientation)) 
    { 
     [self ArrangeControllsFor_Protrate]; 
    } 
    else 
    { 
     [self ArrangeControllsFor_LandScape]; 
    } 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 

    switch (interfaceOrientation) { 

     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 
      //[self ArrangeControllsFor_Protrate]; 
      [self performSelector:@selector(ArrangeControllsFor_Protrate) withObject:Nil afterDelay:0.005f]; 
      return YES; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      [self performSelector:@selector(ArrangeControllsFor_LandScape) withObject:Nil afterDelay:0.005f]; 
      return YES; 
      break; 
    } 
} 
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    //switch ([UIApplication sharedApplication].statusBarOrientation) { 
    switch (toInterfaceOrientation) { 

     case UIInterfaceOrientationPortrait: 
     case UIInterfaceOrientationPortraitUpsideDown: 
      //[self ArrangeControllsFor_Protrate]; 
      [self performSelector:@selector(ArrangeControllsFor_Protrate) withObject:Nil afterDelay:0.005f]; 
      break; 
     case UIInterfaceOrientationLandscapeLeft: 
     case UIInterfaceOrientationLandscapeRight: 
      [self performSelector:@selector(ArrangeControllsFor_LandScape) withObject:Nil afterDelay:0.005f]; 
      break; 
    } 
} 
-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAll; 
} 
-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 
-(void)ArrangeControllsFor_Protrate 
{set frames here. 
} 
-(void)ArrangeControllsFor_LandScape 
{set frames here. 
} 
1

메이트는 별도의 방법으로 당신의 회전 코드를 추가하고있는 viewDidLoad에서 호출하고이 방향을 변경할 때. 아래 코드처럼 :

-(void)viewDidLoad{ 
    [self rotateTheView]; 
} 

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

    [self rotateTheView]; 
} 

- (void) rotateTheView{ 
    int Height = 200; 
    int Yposition = 20; 
    for (int k=0; k<5; k++) 
    { 
     if ([UIApplication sharedApplication].statusBarOrientation== UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) { 

      UIButton *loPlayButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
      [loPlayButton addTarget:self action:@selector(PlayButtonAction:)  forControlEvents:UIControlEventTouchUpInside]; 
      // loPlayButton.tag = 100+k; 
      NSLog(@"tag is %i",loPlayButton.tag); 
      loPlayButton.alpha = 1.0; 

     UIImage *image=[UIImage imageNamed:@"vid.png"]; 
     [loPlayButton setBackgroundImage:image forState:UIControlStateNormal]; 
     loPlayButton.frame=CGRectMake(520, Yposition +(k*Height +170), image.size.width, 30); 
     // loPlayButton.tag=3; 


     [mScrollView_por addSubview:loPlayButton]; 
     [mPlayButtonsArray addObject:loPlayButton]; 
    } 



    if ([UIDevice currentDevice].orientation==UIInterfaceOrientationPortrait || [UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown) { 


     UIButton *loPlayButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
     [loPlayButton addTarget:self action:@selector(PlayButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
      // loPlayButton.tag = 100+k; 
     NSLog(@"tag is %i",loPlayButton.tag); 
     loPlayButton.alpha = 1.0; 

     UIImage *image=[UIImage imageNamed:@"vid.png"]; 
     [loPlayButton setBackgroundImage:image forState:UIControlStateNormal]; 
     loPlayButton.frame=CGRectMake(440, Yposition +(k*Height +170), image.size.width, 30); 
      // loPlayButton.tag=3; 


     [mScrollView_por addSubview:loPlayButton]; 
     [mPlayButtonsArray addObject:loPlayButton]; 

    } 
    } 
} 
0

당신은 의 viewDidLoad 수동으로 전화 안된다. 그거 하지마.

-(void)viewWillAppear:(BOOL)animated 
{ 

    [self resetFrame:[[UIApplication sharedApplication]statusBarOrientation]]; 

} 

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [self resetFrame:toInterfaceOrientation]; 
} 


- (void) resetFrame: (UIInterfaceOrientation)orientation 
{ 


if (UIInterfaceOrientationIsPortrait(orientation)) 
{ 
    // For portrait 

} 
else 
{ 

    // for landscape 

} 
} 
+0

없이 이런 일이되지 ... – user2102546

0

먼저 당신은 당신의 스크롤에서 새 5있는 UIButton을 추가하기 전에 모든있는 UIButton을 제거해야합니다.

for (UIView *button in mScrollView_por.subviews) { 
    if([button isKindOfClass:[UIButton class]]) { 
     [button removeFromSuperview]; 
    } 
} 
+0

그것이 오류가 발생합니다 ... UIView의에 대한 가시적 @ 인터페이스가 선택 알려주고 IsKindOf를 선언하지] – user2102546

+0

은 [버튼 isKindOfClass인가 ...] – Diego

+0

나중에 ... 이유를 오류가오고 있습니까? – user2102546