每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除Constraints
并重新添加但没有运气。
我已经在Stack Overflow和iOS开发论坛上查看了很多相关问题,但我仍然没有解决方案......
我读了一些使用app -(void)viewDidAppear:(BOOL)animated
的地方,但是一旦我使用这段代码设置了大小:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self updateCustomHeight];
}
-(void)updateCustomHeight
{
[self updateViewConstraints];
CGFloat _expandedHeight = 250;
NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:_expandedHeight];
[self.view removeConstraint: _heightConstraint];
[self.view layoutIfNeeded];
if(isPortrait)
{
CGFloat _expandedHeight = 230;
NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
}
else
{
CGFloat _expandedHeight = 200;
NSLayoutConstraint *_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: _expandedHeight];
[self.view addConstraint: _heightConstraint];
}
}
但是,当我加载自定义键盘时,设置第一次设置框架显示横向和纵向。
肖像:
风景:
当我的方向更改委托时,我调用了更改大小的方法:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
//Keyboard is in Portrait
isPortrait=YES;
[self LoadKeyboardview];
[self updateCustomHeight];
}
else{
isPortrait=NO;
[self LoadKeyboardview];
[self updateCustomHeight];
//Keyboard is in Landscape
}
}
注意:
对于键盘布局我正在使用 XIB 并加载nib作为par设备方向,所有工作正常,但设备方向的大小更新无法正常工作。请帮帮我
答案 0 :(得分:1)
添加高度约束时,需要将其保留在属性中,以便随后将其删除。
@property (strong,nonatomic) NSLayoutConstraint *heightConstraint;
-(void)updateCustomHeight
{
[self updateViewConstraints];
CGFloat expandedHeight = 250;
if (self.heightConstraint != nil) {
[self.view removeConstraint:self.heightConstraint];
[self.view layoutIfNeeded];
if(isPortrait)
{
expandedHeight = 230;
}
else
{
expandedHeight = 200;
}
self.heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant: expandedHeight];
[self.view addConstraint:self.heightConstraint];
[self.view layoutIfNeeded];
}
答案 1 :(得分:0)
尝试添加两个约束,一个用于纵向,一个用于横向
- (void)updateViewConstraints {
[super updateViewConstraints];
if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height)
{
[self.view removeConstraint:_heightConstraintforLandscape];
}
else
{
[self.view removeConstraint:_heightConstraintforPortrait];
}
}
我能够解决问题。问题是由于优先权。即使您设置了不同的优先级,它也会考虑初始值,我不知道为什么会这样。
无论如何为景观和肖像设置不同的约束帮助我保持自定义高度。 希望它可以帮助某人。