0

I have tried this :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [self adjustViewsForOrientation:toInterfaceOrientation];
}
- (void)adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
    intervalLabel.center = CGPointMake(100, 100);
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        intervalLabel.center = CGPointMake(200, 200);

    }
}

But it didn't work. however, if I do this it will move it

- (IBAction)sup:(id)sender {
    intervalLabel.center = CGPointMake(100, 100);
}

BTW The label is made in storyboard and is outleted to my view did load and set to (weak, nonatomic) and the button is also made in storyboards and is just a standard IBAction

4

1 回答 1

0

一个问题是你没有打电话:

[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

但是,我猜真正的问题是您正在使用自动布局(有意或无意地),然后试图弄乱中心,这实际上不起作用。

所以我的建议 - 如果您更喜欢使用设置框架和中心的“旧方法”(单击视觉设计器中的 UIViewController 并取消选中“使用自动布局”),请禁用自动布局,或者使用自动布局执行您尝试的操作(意思是添加/在旋转时更改约束)。

在情节提要编辑器中为我的 UIViewController 取消选中“使用自动布局”后,以下内容对我有用:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        _intervalLabel.center = CGPointMake(0, 0);
    } else {
        _intervalLabel.center = CGPointMake(50, 50);
    }    
}

话虽这么说,使用 Autolayout 将是我的建议。

于 2013-09-30T20:49:12.583 回答