iphone - LandScape/Portrait 尺寸不同 iOS

标签 iphone ios landscape landscape-portrait

我有五个 UIButton,我需要设置 UIButton 的框架,使其适合 Landscape肖像 不同的大小。 但是从下面的代码 viewDidLoad 加载正常但问题是加载堆栈后当我移动到 LandscapeportraitportraitLandscape 它应该根据我设置的 viewdidload 大小进行更改..但这并没有发生...它重叠为previous 按钮已经创建....我需要做哪些更改以便在更改 View 时..按钮应该正确显示..不应重叠。

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

相关文章:

ios - 使用 Storyboard 中的自动布局更改横向 View 框架

安卓用户界面 : Multiple Horizontally Paged Table

iphone - 强制 iPhone Web 应用程序进入横向模式

ios - Swift - 将文本添加到可调整大小的图像

ios - launchctl 不适用于 iOS 应用程序

iphone - 如何使用 Xcode 制作首次启动 iPhone App 的导览

objective-c - GameKit 与 GKMatchSendDataReliable 的通信是否可靠?

ios - UITextField/UILabel 和下面的按钮之间的间隙(Xcode Storyboard)

iPhone - UIslider 拇指有多大?

ios - 无法通过Facebook iOS SDK连接到Facebook