【发布时间】: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