我有五个UIButton,我需要以UIButtonLandscape不同的大小设置portrait的框架。
但是从下面的代码viewDidLoad加载它很好,但问题是在加载堆栈后,当我将Landscape移到portraitportrait移到Landscape时,应根据我已设置的大小..但是这没有发生...由于先前的Button已经创建,它已经重叠了....我需要做哪些更改,以便在更改视图时..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];
}

最佳答案

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

关于iphone - LandScape/Portrait尺寸与iOS不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15112487/

10-12 02:55