我想在iOS 8中制作自定义键盘。我希望在手机旋转到横向模式时更改它的大小。
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
if ((toInterfaceOrientation==UIInterfaceOrientationPortrait) || (toInterfaceOrientation==UIInterfaceOrientationPortrait))
{
[def setObject:@"No" forKey:@"LandScape"];
[def synchronize];
islandscapemode = NO;
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: 266];
[self.view addConstraint:_heightConstraint];
}
else
{
[def setObject:@"Yes" forKey:@"LandScape"];
[def synchronize];
NSLayoutConstraint *_heightConstraint =
[NSLayoutConstraint constraintWithItem: self.view
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 0.0
constant: 266];
[self.view addConstraint:_heightConstraint];
self.view.backgroundColor=[UIColor greenColor];
islandscapemode = YES;
}
// [self determineKeyboardNib:toInterfaceOrientation];
}
但它不起作用。我该怎么办?
答案 0 :(得分:1)
willRotateToInterfaceOrientation:duration:
在iOS 8中已弃用。请尝试使用
viewWillTransitionToSize:withTransitionCoordinator:
iOS Developer Library: viewWillTransitionToSize
此函数提供对容器视图的新大小和转换协调器的引用。然后,您可以将键盘大小调整为新的屏幕大小。
还有一个附加到转换协调器的功能,允许您在屏幕旋转时为视图设置动画和/或在屏幕旋转后完成功能块。
iOS Developer Library: UIViewControllerTransitionCoordinator
animateAlongsideTransition:completion:
和
animateAlongsideTransitionInView:animation:completion:
至于你原来的问题,在设置约束后你是否尝试在视图上调用layoutIfNeeded?