2013-02-27 3 views
0

У меня есть пять UIButton, и мне нужно установить кадр UIButton таким образом, чтобы он был для Landscape и portrait разных размеров. но из приведенной ниже коды viewDidLoad его загрузка тонкой, но проблема в том, что после загрузки стеки Когда я перехожу к Landscape к portraitportrait или к 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

Mate добавить свой код поворота в отдельный метод и вызвать его из 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

Прежде всего, необходимо удалить все UIButton перед добавлением нового 5 UIButton из вашей свитки.

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

он бросает ошибку ... не видно @interface для UIView не объявляет iskindof селектор] – user2102546

+0

кнопка [isKindOfClass: ...] – Diego

+0

я ... почему это ошибка идет? – user2102546