【问题标题】:Is it possible that keyboard change iOS app orientation?键盘是否有可能改变 iOS 应用程序的方向?
【发布时间】:2015-01-03 00:14:27
【问题描述】:

我有问题。

在我的应用程序中,我有一个包含一些文本字段的视图。 我使用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];}

【问题讨论】:

  • “键盘是否有可能改变 iOS 应用程序的方向” 不,不是。你一定做错了什么。如果您需要帮助,请显示代码。 - 顺便说一句,在 iOS 8 中你不应该使用 willRotate(但如果这个应用程序也在 iOS 7 中运行,你需要它)。
  • 优秀。现在:“我点击一些文本字段并显示键盘。之后文本字段框架返回纵向模式”这是否意味着当键盘出现时正在调用您的setFramesForInterface:?如果是这样,您需要找出谁在调用它!设置断点并查看调用堆栈。
  • 我做到了,当我点击 texfield 时,setFramesForInterface: 没有通过任何方法调用。
  • 再次优秀!我已准备好给出答案(可能对或错,但您现在已经给出了很好的信息)。

标签: ios objective-c cocoa-touch


【解决方案1】:

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

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

好吧,你可以使用setFrame:,但这有点没有意义,因为约束是最重要的。当布局出现时,无论出于何种原因,都会遵守约束。所以发生在你身上的是:你改变了框架,但是你触发了布局并且约束接管了,把所有的东西都移回了原来的位置。

【讨论】:

  • 是的,我的回答是建议您更改setFramesForPortraitsetFramesForLandscapeLeft,以便它们更改的是约束,而不是框架。
  • 我未选中“使用自动布局”,它可以工作,谢谢 ;)
  • 不,取消选中使用自动布局不是一个好的解决方案。您应该学习自动布局,而不是避免它。
  • 我知道,但这是一个大学项目,应该这样做;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2018-09-20
  • 1970-01-01
相关资源
最近更新 更多