键盘是否可能改变iOS应用程序方向?

时间:2015-01-02 15:17:59

标签: ios objective-c cocoa-touch

我有问题。

在我的应用程序中,我有一些视图,其中包含一些文本字段。 我使用willRotateToInterfaceOrientation方法进行更改方向。此外,我更新纵向和横向模式的文本框架帧。

当我在应用程序中设置横向模式时,一切都很有效,直到我点击一些文本字段并且键盘出现。之后,文本字段帧返回“纵向”模式(但只有文本字段和标签的帧)。

当我在父视图中旋转到横向并使用文本字段转到我的视图时,也会发生这种情况。

键盘是否可能改变iOS应用程序方向?我会很乐意提供一些建议。

代码(我在父视图和第二视图中执行):

在viewDidLoad中:

[self setFramesForInterface:self.interfaceOrientation];        

方法:

- (void)setFramesForInterface:(UIInterfaceOrientation)toInterfaceOrientation {

switch (toInterfaceOrientation) {
    case UIInterfaceOrientationUnknown:
        [self setFramesForPortrait];
        break;
    case UIInterfaceOrientationPortrait:
        [self setFramesForPortrait];
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        [self setFramesForPortrait];
        break;
    case UIInterfaceOrientationLandscapeLeft:
        [self setFramesForLandscapeLeft];
        break;
    case UIInterfaceOrientationLandscapeRight:
        [self setFramesForLandscapeLeft];
        break;
    default:
        break;
} }

- (void)setFramesForPortrait {
dispatch_async(dispatch_get_main_queue(), ^{
    [_stationNameLabel setFrame:CGRectMake(8, 40, 105, 30)];
    [_addresLabel setFrame:CGRectMake(8, 85, 105, 30)];
    .........................
}); }

- (void)setFramesForLandscapeLeft {
dispatch_async(dispatch_get_main_queue(), ^{
    [_stationNameLabel setFrame:CGRectMake(8, 20, 105, 30)];
    [_addresLabel setFrame:CGRectMake(8, 65, 105, 30)];
    ...................
}); }

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

1 个答案:

答案 0 :(得分:1)

我猜你正在使用自动布局。这就是问题的根源。

使用自动布局时,无法使用setFrame:重新定位/调整大小。您必须改为更改约束

嗯,你可以使用setFrame:,但这有点毫无意义,因为约束是重要的。当布局出现时,出于任何原因,遵守约束。所以你发生的事情是:你改变了框架,然后你就会触发布局并接受约束,将一切都移回原来的位置。

相关问题