gpt4 book ai didi

ios - 当设备方向改变时,自定义键盘高度不会改变

转载 作者:行者123 更新时间:2023-11-29 12:36:11 24 4
gpt4 key购买 nike

每当我旋转设备时,我的自定义键盘高度始终保持不变。我尝试删除 Constraints 并再次添加它,但没有成功。我已经在 Stack Overflow 和 iOS 开发论坛上查看了很多相关问题,但我仍然没有解决方案..

我读了一些使用过的应用程序 -(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];
}

}

但是当我加载自定义键盘时,设置第一次设置框架显示为横向和纵向。

纵向:

enter image description here

风景:

enter image description here

我在我的方向更改委托(delegate)时调用该更改大小方法:

-(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 加载为标准设备方向,所有这些都工作正常,但在设备方向中更新大小将无法正常工作。请帮我解决这个问题

最佳答案

当您添加高度约束时,您需要将其保存在一个属性中,以便您随后可以将其删除。

@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];
}

关于ios - 当设备方向改变时,自定义键盘高度不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26275195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com