LandScape/Portrait size differ iOS

2019-09-10 02:50发布

问题:

I have five UIButton and I need to set the frame of the UIButton in such a way that it should be for the Landscape and portrait different size. but the from the Below code viewDidLoad its loading fine but problem is that After loading the stack When I move to the Landscape to portrait or the portrait to Landscape it should be change according to the viewdidload size which i have set ..but this is not happening...its been overlapping as previous Button is already created....what changes do i require so that when change the view ..button should come properly ..should not be overlap.

-(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.
}


回答2:

Mate add the your rotation code in separate method and call it from viewDidLoad and when it changes the orientation. Like in the below code :

-(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:

You are not supposed to call viewDidLoad manually. Do not do that.

 -(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:

First you need remove all UIButton before adding the new 5 UIButton from your scroll.

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