如何在设备更改方向(纵向,横向)时立即检测UIInterfaceOrientation

时间:2014-03-19 01:58:01

标签: objective-c uitableview uibutton uiinterfaceorientation

我创建UITableViewCell,我想在用户将横向更改为纵向或纵向横向时更改按钮位置我尝试检查相同的此代码但不会自动检测。

    - CellForRowAtIndexPath
{
    if (UIDeviceOrientationIsPortrait(self.interfaceOrientation))
    {
        [Button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 50, 20, 50, 50)];
    }else
    {
        [Button setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.height - 70, 20, 50, 50)];
    }
}

1 个答案:

答案 0 :(得分:1)

一种方法是将这些添加到viewController:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // will rotate
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    // will animate rotation
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // done rotating
}

或者,您可以注册通知:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationDidChange:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:self];