2013-02-27 65 views
0

我有五个UIButton,我需要设置UIButton的框架,使其应该为Landscapeportrait不同的大小。 但是从下面的代码viewDidLoad它的加载不错,但问题是,加载堆栈后当我移动到LandscapeportraitportraitLandscape就应该按照我所设置..但是这是viewdidload大小是变化没有发生......它已经重叠,因为以前的Button已经创建了....我需要做什么更改,以便在更改视图时..button应该正常显示..不应该重叠。LandScape /肖像大小不同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 2013-02-27 13:08:20

0

首先,你需要从您的滚动增加新5 UIButton的前删除所有的UIButton。

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

,它抛出一个错误...为的UIView没有明显的@interface声明选择iskindof] – user2102546 2013-02-27 13:13:46

+0

是[按钮isKindOfClass:...] – Diego 2013-02-27 13:15:24

+0

亚...为什么这个错误即将到来? – user2102546 2013-02-27 13:23:14

相关问题