更改标签后 UIToolBar 恢复
UIToolBar reverts after changing a label
我有一个 UIToolBar,里面有一个 UITextField,还有一个标签。我试图让标签在用户键入时更新,以便他们知道他们键入了多少个字符。
当前,当我尝试更新标签计数器时,UIToolBar returns 回到其原始位置。 Here is a gif showing the issue I'm having.
我所做的是:
-(IBAction)CharCount:(id)sender{
NSString *substring = textField.text;
NSString *limitHit;
limitHit = substring;
int maxChar = 160;
if (limitHit.length > 0) {
TextCounter.hidden = NO;
TextCounter.text = [NSString stringWithFormat:@"%d/160", limitHit.length];
}
}
如何在不反转动画以将工具栏与键盘一起移动的情况下更新标签?
========================编辑==================== ====
不使用自动布局意味着我对 iPhone 4S 的看法是错误的。他们是下面的例子。底部的菜单挂起。我该如何设置它才不会发生这种情况?
这一切看起来都可以通过将 UIToolbar
设置为 UITextview
的 inputAccessoryView
来简化和解决。这会将工具栏附加到键盘,因为它会上下动画。如果您希望它保留在视图控制器中的视图底部,您可以覆盖视图控制器的inputAccessoryView
,然后将此方法添加到视图控制器的实现文件中:
- (BOOL)canBecomeFirstResponder {
return YES;
}
Here 是在视图控制器上使用 inputAccessoryView 的便捷介绍。
不要关闭自动布局,只需更改约束而不是框架。由于 layoutSubviews 方法,无法使用自动布局更改框架。这个方法在很多情况下都是由系统调用的。您需要:
向工具栏添加底部约束:
订阅键盘通知。
当键盘显示或隐藏时更改工具栏的底部约束。
代码示例:
- (void)dealloc {
[self unsubscribeForKeyboardNotifications];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self subscribeForKeyboardNotifications];
}
#pragma mark - Keyboard notifications
- (void)subscribeForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unsubscribeForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillAppear:(NSNotification *)notification {
CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[self changeToolbarBottomConstraintWithConstant:keyboardHeight];
}
- (void)keyboardWillDisappear:(NSNotification *)notification {
[self changeToolbarBottomConstraintWithConstant:0];
}
- (void)changeToolbarBottomConstraintWithConstant:(CGFloat)constant {
[self.toolBar.superview.constraints enumerateObjectsUsingBlock:
^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (constraint.secondItem == self.toolBar && constraint.secondAttribute == NSLayoutAttributeBottom)
constraint.constant = constant;
}];
[UIView animateWithDuration:0.5
animations:^{
[self.view layoutIfNeeded];
}];
}
结果:
无需删除自动布局只需添加两个约束尾随 space 到 toolbarview 并固定宽度约束
希望这对你有帮助我有类似的问题,我用这种方式解决了。
您也可以通过设置框架在没有自动布局的情况下实现。在名为 InputView
的视图中获取文本字段和标签,并将其添加到 self.view 和您的 textField
中作为 tfInput
.
现在在您的视图控制器中为文本字段设置委托。
然后,根据需要改变视图的Y位置即可。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField== tfInput)
{
InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 216 - InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
}
return YES;
}
和
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField== tfInput)
{
InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 49 InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
}
return YES;
}
这里我把工具栏的大小设置为49,也可能是你自定义的大小。
你也可以在设置框架时做一些动画。
这是框架集的一个选项。
第二个选项是将它放在滚动视图和相同的文本字段委托方法中 textFieldShouldBeginEditing
你必须将内容偏移量设置到你需要的位置并在 textFieldShouldReturn
.[=17= 中将其设置为 0 ]
我有一个 UIToolBar,里面有一个 UITextField,还有一个标签。我试图让标签在用户键入时更新,以便他们知道他们键入了多少个字符。
当前,当我尝试更新标签计数器时,UIToolBar returns 回到其原始位置。 Here is a gif showing the issue I'm having.
我所做的是:
-(IBAction)CharCount:(id)sender{
NSString *substring = textField.text;
NSString *limitHit;
limitHit = substring;
int maxChar = 160;
if (limitHit.length > 0) {
TextCounter.hidden = NO;
TextCounter.text = [NSString stringWithFormat:@"%d/160", limitHit.length];
}
}
如何在不反转动画以将工具栏与键盘一起移动的情况下更新标签?
========================编辑==================== ====
不使用自动布局意味着我对 iPhone 4S 的看法是错误的。他们是下面的例子。底部的菜单挂起。我该如何设置它才不会发生这种情况?
这一切看起来都可以通过将 UIToolbar
设置为 UITextview
的 inputAccessoryView
来简化和解决。这会将工具栏附加到键盘,因为它会上下动画。如果您希望它保留在视图控制器中的视图底部,您可以覆盖视图控制器的inputAccessoryView
,然后将此方法添加到视图控制器的实现文件中:
- (BOOL)canBecomeFirstResponder {
return YES;
}
Here 是在视图控制器上使用 inputAccessoryView 的便捷介绍。
不要关闭自动布局,只需更改约束而不是框架。由于 layoutSubviews 方法,无法使用自动布局更改框架。这个方法在很多情况下都是由系统调用的。您需要:
向工具栏添加底部约束:
订阅键盘通知。
当键盘显示或隐藏时更改工具栏的底部约束。
代码示例:
- (void)dealloc {
[self unsubscribeForKeyboardNotifications];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self subscribeForKeyboardNotifications];
}
#pragma mark - Keyboard notifications
- (void)subscribeForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unsubscribeForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillAppear:(NSNotification *)notification {
CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
[self changeToolbarBottomConstraintWithConstant:keyboardHeight];
}
- (void)keyboardWillDisappear:(NSNotification *)notification {
[self changeToolbarBottomConstraintWithConstant:0];
}
- (void)changeToolbarBottomConstraintWithConstant:(CGFloat)constant {
[self.toolBar.superview.constraints enumerateObjectsUsingBlock:
^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (constraint.secondItem == self.toolBar && constraint.secondAttribute == NSLayoutAttributeBottom)
constraint.constant = constant;
}];
[UIView animateWithDuration:0.5
animations:^{
[self.view layoutIfNeeded];
}];
}
结果:
无需删除自动布局只需添加两个约束尾随 space 到 toolbarview 并固定宽度约束 希望这对你有帮助我有类似的问题,我用这种方式解决了。
您也可以通过设置框架在没有自动布局的情况下实现。在名为 InputView
的视图中获取文本字段和标签,并将其添加到 self.view 和您的 textField
中作为 tfInput
.
现在在您的视图控制器中为文本字段设置委托。
然后,根据需要改变视图的Y位置即可。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField== tfInput)
{
InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 216 - InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
}
return YES;
}
和
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField== tfInput)
{
InputView.frame = CGRectMake(InputView.frame.origin.x,self.view.frame.size.height - 49 InputView.frame.size.height,InputView.frame.size.width,InputView.frame.size.height);
}
return YES;
}
这里我把工具栏的大小设置为49,也可能是你自定义的大小。 你也可以在设置框架时做一些动画。
这是框架集的一个选项。
第二个选项是将它放在滚动视图和相同的文本字段委托方法中 textFieldShouldBeginEditing
你必须将内容偏移量设置到你需要的位置并在 textFieldShouldReturn
.[=17= 中将其设置为 0 ]