代码之家  ›  专栏  ›  技术社区  ›  user2102546

LandScape/人像尺寸不同iOS

  •  0
  • user2102546  · 技术社区  · 11 年前

    我有五个 UIButton 我需要设置 UI按钮 以这样一种方式,它应该是为了 Landscape portrait 不同尺寸。 但来自以下代码 viewDidLoad 它的加载很好,但问题是在加载堆栈之后,当我移动到 景观 肖像 或者 肖像 景观 应该根据 viewdidload 我设定的尺寸。。但这并没有发生。。。它一直在重叠,因为上一个按钮已经创建。。。。我需要做什么更改,以便在更改视图时。。按钮应该正确。。不应重叠。

    -(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];
    }
    
    4 回复  |  直到 11 年前
        1
  •  1
  •   Rahul    11 年前
    - (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
  •  1
  •   Ushan87    11 年前

    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
  •  0
  •   Anil Varghese    11 年前

    你不应该打电话 查看DidLoad 手动。不要那样做。

     -(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
  •  0
  •   Diego    11 年前

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

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