【问题标题】:LandScape/Portrait size differ iOS横向/纵向大小不同 iOS
【发布时间】:2013-02-27 12:45:47
【问题描述】:

我有五个UIButton,我需要设置UIButton 的框架,使其适合Landscape 和portrait 不同的大小。 但是从下面的代码viewDidLoad 它的加载很好但问题是加载堆栈后当我移动到Landscape 到portrait 或portrait 到Landscape 它应该根据@987654330 进行更改@我设置的大小..但这没有发生......它已经重叠,因为之前的按钮已经创建......我需要进行哪些更改,以便在更改视图时..按钮应该正确..不应该重叠。

-(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];
}

【问题讨论】:

    标签: iphone ios landscape landscape-portrait


    【解决方案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.
    }
    

    【讨论】:

      【解决方案2】:

      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];
      
          }
        }
      }
      

      【讨论】:

        【解决方案3】:

        您不应该手动调用 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
        
        }
        }
        

        【讨论】:

          【解决方案4】:

          首先,您需要先移除所有 UIButton,然后再从滚动条中添加新的 5 UIButton。

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

          【讨论】:

          • 它抛出一个错误...没有可见的 UIView @interface 声明选择器 iskindof]
          • 是 [按钮 isKindOfClass:...]
          猜你喜欢
          • 2011-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-14
          • 2014-11-28
          • 2016-06-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多