2014-09-25 75 views
1

我正在为iOS 8编写自定义键盘。我的主UIView在我的viewDidLoad方法中返回{{0, 0}, {0, 0}}。我试图用view.frame和bounds接收CGRect,但都返回0.但是,如果我使用这些相同的方法来检索我的视图在viewDidAppear方法中的坐标,它们将返回正常:{{0, 0}, {320, 216}}。这里的问题是viewDidAppear在didLoad之后运行,所以我无法从didAppear检索大小并在didLoad中使用它。这是我的问题:iOS 8自定义键盘self.view.frame返回0

为什么self.view的帧在viewDidLoad返回0?它的大小不应该在这里设置?这(viewDidLoad)是我应该做的大部分启动和UI设置的地方,对吗?我在Xcode中有经验。

我在swift编程,虽然这应该没关系(?)我认为这值得注意。在Obj-C中的答案很好。

编辑:我创建了一个全新的Xcode项目和目标下面的类并收到同样的错误:

import UIKit 

class KeyboardViewController: UIInputViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    println(self.view.frame) // returns (0.0,0.0,0.0,0.0) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

override func viewDidAppear(animated: Bool) { 
    println(self.view.frame) // returns (0.0,0.0,320.0,216.0) 
} 


} 

我用模拟器和物理设备也从6到4S测试。没有什么似乎有所作为。

+0

我在这里遇到麻烦,那个println无法在控制台日志中打印任何东西! – TomSawyer 2014-10-20 15:35:17

回答

0

我能得到它通过增加在顶部的文本框,并使其FirstResponder工作:

进口的UIKit

类KeyboardViewController:UIInputViewController {

@IBOutlet var textField: UITextField! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    textField.becomeFirstResponder() 
    println(self.view.frame) // returns (0.0,0.0,375.0,667.0) 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 

override func viewDidAppear(animated: Bool) { 
    println(self.view.frame) // returns (0.0,0.0,375.0,667.0) 

} 

}

0

你应该用于定制键盘的self.inputView.frame而不是self.view.frame

您可以使用isPortraitLayout方法来检查/如果横向模式或纵向模式初始化视图...

-(BOOL) isPortraitLayout{ 
    int appExtensionWidth = (int)round(self.view.frame.size.width); 

    int possibleScreenWidthValue1 = (int)round([[UIScreen mainScreen] bounds].size.width); 
    int possibleScreenWidthValue2 = (int)round([[UIScreen mainScreen] bounds].size.height); 

    int screenWidthValue; 

    if (possibleScreenWidthValue1 < possibleScreenWidthValue2) { 
     screenWidthValue = possibleScreenWidthValue1; 
    } else { 
     screenWidthValue = possibleScreenWidthValue2; 
    } 

    return (appExtensionWidth == screenWidthValue); 
} 
+0

这仍然在viewDidLoad下返回0,但不在viewDidAppear下。以下是澄清的截图:http://i.imgur.com/SJib6WZ。png – Alex 2014-09-27 19:54:07

+0

我注意到你还没有将任何视图(或Xibs)分配给inputView或view。一旦你分配它(即self.inputView = self.Keyboard,其中self.keyboard是view/xib),你将得到帧值。 – 2014-09-29 08:49:06

0

它是视图显示层级(不只是键盘的扩展)的一般特性,即帧大小在viewDidLoad中不正确。请参阅herehere,其中也提供了关于如何处理布局的一些建议。

我会推荐使用自动布局(类似于在键盘扩展示例代码中完成“nextKeyboardButton”),在这种情况下,大多数情况下您不必担心处理帧更改。例如。在viewDidLoad中

MyKeyboardView* keyboardView = [[keyboardView alloc] init]; 
    keyboardView.translatesAutoresizingMaskIntoConstraints = NO; 

    [self.inputView addSubview: keyboardView]; 

    NSLayoutConstraint *leftSideConstraint = [NSLayoutConstraint constraintWithItem:keyboardView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.inputView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]; 
    NSLayoutConstraint *rightSideConstraint = [NSLayoutConstraint constraintWithItem:keyboardView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.inputView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]; 
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:keyboardView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.inputView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]; 
    NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:keyboardView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.inputView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]; 

    [self.inputView addConstraints:@[leftSideConstraint, rightSideConstraint, bottomConstraint, topConstraint]]; 

通过这种方式,框架为您keyboardView将跟踪inputView框(设置为默认键盘边框尺寸的方向和设备),并因此与方向的变化自动调整。然后,您可以在MyKeyboardView类的layoutSubviews中处理进一步的自定义布局。最后一件事,如果您的MyKeyboardView在drawRect中执行任何自定义绘图,则需要在MyKeyboardView的init self.contentMode = UIViewContentModeRedraw;中设置contentMode,否则在更改框架时不会调用drawRect。