簡體   English   中英

LandScape / Portrait尺寸與iOS不同

[英]LandScape/Portrait size differ iOS

我有五個UIButton ,我需要設置UIButton的框架,以便它應該是Landscapeportrait不同的大小。 但是從下面的代碼viewDidLoad它的加載很好,但問題是加載堆棧當我移動到Landscapeportrait或者portraitLandscape它應該根據我設置的viewdidload大小改變..但這不是正在發生......它已經重疊,因為之前的Button已經創建了....我需要做哪些更改,以便在更改視圖時..按鈕應該正確...不應該重疊。

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

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

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

您不應該手動調用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

}
}

首先,您需要在從滾動條添加新的5 UIButton之前刪除所有UIButton。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM