【问题标题】:Change UIImageView position when rotate view旋转视图时更改 UIImageView 位置
【发布时间】:2013-01-28 22:51:42
【问题描述】:

当我旋转设备时,我正在尝试更改 iPad 中 UIImageView 的位置,但我不明白为什么有时会发生变化而有时不会发生变化,我还想检测视图何时开始方向,所以我可以设置正确的位置,但不起作用,这是代码:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"%d",toInterfaceOrientation);
    [self performSelectorOnMainThread:@selector(changeShelfPosition:) withObject:[NSNumber numberWithInt:toInterfaceOrientation] waitUntilDone:YES];
}

-(void)changeShelfPosition:(NSNumber *)orientation
{
    if (orientation.integerValue == 1 ) {

        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);

    } else if (orientation.integerValue == 2) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 544, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    } else if (orientation.integerValue == 3) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    } else if (orientation.integerValue == 4) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, 594, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
}

- (void)viewDidLoad
{
...
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == 0) {

    }//Default orientation
        //UI is in Default (Portrait) -- this is really a just a failsafe.
    else if(orientation == UIInterfaceOrientationPortrait) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
            //Do something if the orientation is in Portrait
    else if(orientation == UIInterfaceOrientationLandscapeLeft) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
                // Do something if Left
    else if(orientation == UIInterfaceOrientationLandscapeRight) {
        self.shelfImage3.frame = CGRectMake(self.shelfImage3.frame.origin.x, self.shelfImage3.frame.origin.y+50, self.shelfImage3.frame.size.width, self.shelfImage3.frame.size.height);
    }
}

【问题讨论】:

    标签: iphone ios6 uiimageview uiinterfaceorientation


    【解决方案1】:

    您无法访问 viewDidLoad 中的框架或其他几何属性并期望得到一致的结果,因为尚未设置视图几何。

    改用viewWillAppear

    【讨论】:

    • 好的,为什么在这种方法中也不起作用 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    • 我发现了问题,这是 iOS 6 的自动布局选项,我在界面生成器中禁用了它...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多