ios - UIToolBar 在更改标签后恢复
<p><p>我有一个 UIToolBar,里面有一个 UITextField,还有一个标签。我试图让标签在用户输入时更新,以便他们知道他们输入了多少个字符。 </p>
<p>当前,当我尝试更新标签计数器时,UIToolBar 会返回到其原始位置。 <a href="https://gfycat.com/NeatSlipperyAssassinbug" rel="noreferrer noopener nofollow">Here is a gif showing the issue I'm having.</a> </p>
<p>我正在做的事情如下:</p>
<pre><code>-(IBAction)CharCount:(id)sender{
NSString *substring = textField.text;
NSString *limitHit;
limitHit = substring;
int maxChar = 160;
if (limitHit.length > 0) {
TextCounter.hidden = NO;
TextCounter.text = ;
}
}
</code></pre>
<p>如何在不反转动画以将工具栏与键盘一起移动的情况下更新标签?</p>
<p>=========================编辑====================== ==</p>
<p>不使用自动布局意味着我对 iPhone 4S 的看法是错误的。他们是下面的一个例子。底部的菜单挂起。我该如何设置它才不会发生?</p>
<p> <img src="/image/OvNTd.png" alt="example iPhone 4S"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>不要关闭自动布局,只需更改约束而不是框架。由于 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instm/UIView/layoutSubviews" rel="noreferrer noopener nofollow">layoutSubviews</a>,无法使用自动布局更改框架方法。该方法在很多情况下被系统调用。你需要:</p>
<ol>
<li><p>为您的工具栏添加底部约束:</p>
<p> <img src="/image/Sl2Ln.png" alt="enter image description here"/> </p></li>
<li><p>订阅键盘通知。</p></li>
<li><p>在键盘显示或隐藏时更改工具栏的底部约束。</p></li>
</ol>
<p>代码示例:</p>
<pre><code>- (void)dealloc {
;
}
- (void)viewDidLoad {
;
;
}
#pragma mark - Keyboard notifications
- (void)subscribeForKeyboardNotifications {
[ addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[ addObserver:self
selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)unsubscribeForKeyboardNotifications {
[ removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[ removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillAppear:(NSNotification *)notification {
CGFloat keyboardHeight = CGRectValue].size.height;
;
}
- (void)keyboardWillDisappear:(NSNotification *)notification {
;
}
- (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:^{
;
}];
}
</code></pre>
<p>结果:</p>
<p> <img src="/image/s3UrH.gif" alt="enter image description here"/> </p></p>
<p style="font-size: 20px;">关于ios - UIToolBar 在更改标签后恢复,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29442180/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29442180/
</a>
</p>
页:
[1]