所以我一直在使用这个自定义键盘,直到iOS 8 Beta 3一切正常。
我为iPhone Portrait,iPhone Landscape,iPad Portrair,iPad Landscape和iPhone 4 Portrait创建了5个xib,这就是问题所在。
在我的KeyboardViewController中:UIInputViewController,它是在我根据模板创建自定义键盘时创建的。
我有这个私有方法来确定我应该加载哪个xib,基本上我只是检查屏幕宽度然后我删除所有子视图并添加新的键盘视图。
- (void)viewWillLayoutSubviews
{
NSLog(@"[DEBUG] viewWillLayoutSubviews");
[super viewWillLayoutSubviews];
[self determineKeyboardNib:self.view.frame.size];
}
//
//- (void)viewDidLayoutSubviews
//{
// NSLog(@"[DEBUG] viewDidLayoutSubviews");
// [super viewDidLayoutSubviews];
// [self determineKeyboardNib:self.view.frame.size];
//}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
NSLog(@"[DEBUG] viewWillTransitionToSize:withTransitionCoordinator:");
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self determineKeyboardNib:size];
}
/*
* Determine which xib file to load based on the width of the screen
*/
- (void) determineKeyboardNib: (CGSize)size
{
NSLog(@"[DEBUG] determineKeyboardNib: Enter if");
KeyboardIBBackend *keyboard_view;
// determine device
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPad");
// iPad
if (size.width > 1000) {
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPad Landscape");
// landscape
keyboard_view = [KeyboardIBBackend keyboardiPadLandscapeView];
}else{
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPad Portrait");
// portrait
keyboard_view = [KeyboardIBBackend keyboardiPadPortraitView];
}
}else{
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPhone");
// iPhone
if (size.width > 500){
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPhone Landscape");
// landscape
keyboard_view = [KeyboardIBBackend keyboardiPhone5LandscapeView];
}else if(size.width > 450){
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPhone 4 Portrait");
// portrait
keyboard_view = [KeyboardIBBackend keyboardiPhone4LandscapeView];
}else{
NSLog(@"[DEBUG] determineKeyboardNib: Enter iPhone 5 Portrait");
keyboard_view = [KeyboardIBBackend keyboardiPhonePortraitView];
}
}
NSLog(@"[DEBUG] determineKeyboardNib: Exit if");
keyboard_view.parentview = self;
// remove previous keyboard view
for (UIView *v in [self.view subviews]) {
[v removeFromSuperview];
}
// add the newly determined view
NSLog(@"[DEBUG] determineKeyboardNib: before addSubview");
[self.view addSubview:keyboard_view];
NSLog(@"[DEBUG] determineKeyboardNib: after addSubview");
}
首先,我尝试将此方法放入 - (void)viewWillTransitionToSize:(CGSize)大小withTransitionCoordinator:(id)协调器,但无论Beta 2还是Beta 3都不会被调用。
因此,我将该方法放在viewWillLayoutSubviews:中,它就像Beta 3之前的魅力一样。
在Beta 3中,当我旋转设备时,viewWillLayoutSubviews被无限期调用。我想这是因为在detemineKeyboardNib方法中,最后一行是
[self.view addSubview:keyboard_view]
可能导致它一次又一次地调用viewWillLayoutSubviews。但是,Beta 2中并非如此。
然后我尝试在viewDidLayoutSubviews里面确定KeyboardNib,与上面相同。
所以我想知道在Beta 3中处理设备旋转的正确方法是什么。我相信viewWillTransitionToSize:withTransitionCoordinator:应该是正确的方法但是当我旋转设备时它真的没有被调用...请帮忙这里。
谢谢,
答案 0 :(得分:0)
我不知道你是否已经修好了,但是我发现似乎有效的解决方案是将代码放在if块中,只有在大小发生变化时才运行,并在之后调用super viewWillLayoutSubviews。 / p>
所以这样:
-(void)viewWillLayoutSubviews
{
if(self.size.frame.width != self.someClassVariableToTrackWidth)
{
self.someClassVariableToTrackWidth = self.size.frame.width;
//call stuff here
}
[super viewWillLayoutSubviews];
}
我正在寻找更好的解决方案,因为这总是一种hackish方法,当我发现它时,我会发布它。