gpt4 book ai didi

ios - 如何在 ios 运行时禁用约束?

转载 作者:行者123 更新时间:2023-12-01 16:31:51 27 4
gpt4 key购买 nike

我有一个进一步有一些文本字段的 View 。我已经用 self 及其 super View 设置了 View 的纵横比。现在,当我单击文本字段然后出现键盘时,我正在调整 View 大小并设置其 y 位置,以便键盘不会覆盖文本字段。我为此使用下面的代码。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
/* keyboard is visible, move views */
if([textField isEqual:self.txt_twitter] || [textField isEqual:self.txt_linkedin])
{
[UIView animateWithDuration:0.1f animations:^
{
CGRect rect = self.view.frame;

// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= 130;
rect.size.height += 130;
self.view.frame = rect;



}];
}

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{

/* resign first responder, hide keyboard, move views */
/* keyboard is visible, move views */
if([textField isEqual:self.txt_twitter] || [textField isEqual:self.txt_linkedin])
{
[UIView animateWithDuration:0.1f animations:^
{
CGRect rect = self.view.frame;

// revert back to the normal state.
rect.origin.y += 130;
rect.size.height -= 130;


self.view.frame = rect;


}];

}

}

现在,当我的 View 高度增加时,它的宽度也会增加,因为我已经为此设置了约束。请告诉我如何解决这个问题。在这种情况下,我不想增加 View 的宽度。

最佳答案

混合自动布局约束和直接帧操作通常是一个坏主意,您可能应该避免这种情况。我建议您为要移动/调整大小的 View 的高度或位置约束制作一些 IBOutlets,并在委托(delegate)回调中更改 constraint.constant 而不是更改帧。

此外,在处理键盘事件时,最好收听键盘通知(UIKeyboardWillShowNotification),而不是使用文本字段委托(delegate)方法。

添加了一个 gif 以更好地说明 -

enter image description here

项目中的一些代码:

- (void)viewDidLoad {
[super viewDidLoad];

[self addKeyboardNotificationsObserver];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)addKeyboardNotificationsObserver {

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

- (void)handleKeyboardWillShow:(NSNotification *)paramNotification

{

NSDictionary* info = [paramNotification userInfo];

//when switching languages keyboard might change its height (emoji keyboard is higher than most keyboards).
//You can get both sizes of the previous keyboard and the new one from info dictionary.

// size of the keyb that is about to disappear
__unused CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

// size of the keyb that is about to appear
CGSize kbSizeNew = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

//make adjustments to constraints here...

self.redSquareBottomConstraint.constant = kbSizeNew.height;

//and this is where magic happens!

[self.view layoutIfNeeded];

}

- (void)handleKeyboardWillHide:(NSNotification *)paramNotification

{
//adjust constraints

self.redSquareBottomConstraint.constant = 0;

[self.view layoutIfNeeded];

}

- (void)dealloc {

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

从代码中可以看出,我们从通知附带的 userInfo 字典中提取键盘的大小。正如 所指出的那样恩格里夫 ,您可以从中提取一些其他有值(value)的信息,例如键盘出现/消散时动画的持续时间(key - UIKeyboardAnimationDurationUserInfoKey)或曲线(key - UIKeyboardAnimationCurveUserInfoKey)。文档 here .如果您想要特定的动画行为,您可以将布局调用包装在动画 block 中,如下所示:
[UIView animateWithDuration:duration
animations:^{

[self.view layoutIfNeeded];

}];

关于ios - 如何在 ios 运行时禁用约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31291744/

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