在我的项目中,我使用了这个项目:
DAKeyboardControl:https://github.com/danielamitay/DAKeyboardControl
要将带有UIToolbar的UITextView附加到键盘上方,并在取消键盘时用摇摄手势将其移动,现在我无法理解在编辑文本时如何更改UITextView和UIToolbar的高度,要添加UITextView,我使用DAKeyboardControl示例的相同代码,用UITextView更改UITextField。
现在我如何动态地改变文本视图的高度?
发布于 2014-04-08 12:12:13
将此代码放入viewDidLoad中:
self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f,
self.view.bounds.size.height-40,
self.view.bounds.size.width,
40.0f)];
self.toolBar.barStyle = UIBarStyleBlack;
self.toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.toolBar];
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10.0f,6.0f,self.toolBar.bounds.size.width - 20.0f - 68.0f,30.0f)];
self.textView.clipsToBounds = YES;
self.textView.layer.cornerRadius = 5.0f;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.textView setKeyboardAppearance:UIKeyboardAppearanceAlert];
[self.textView setFont:[UIFont tv_LektonRegular:16]];
[self.textView setDelegate:self];
[self.toolBar addSubview:self.textView];
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[sendButton setTitle:NSLocalizedString(@"Send", @"") forState:UIControlStateNormal];
[sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[sendButton.titleLabel setFont:[UIFont tv_LektonRegular:18]];
[sendButton addTarget:self action:@selector(sendComment:) forControlEvents:UIControlEventTouchUpInside];
sendButton.frame = CGRectMake(self.toolBar.bounds.size.width - 68.0f,
6.0f,
58.0f,
29.0f);
[self.toolBar addSubview:sendButton];
self.view.keyboardTriggerOffset = self.toolBar.bounds.size.height;
__weak CommentActivityViewController* weakSelf = self;
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
CGRect toolBarFrame = weakSelf.toolBar.frame;
toolBarFrame.origin.y = keyboardFrameInView.origin.y - toolBarFrame.size.height;
weakSelf.toolBar.frame = toolBarFrame;
}];然后:
- (void)textViewDidChange:(UITextView *)textView
{
[self _updateInputViewFrameWithKeyboardFrame];
}
- (void)_updateInputViewFrameWithKeyboardFrame
{
// Calculate the height the input view ideally
// has based on its textview's content
UITextView *textView = self.textView;
CGFloat newInputViewHeight;
if ([NSURLSession class])
{
newInputViewHeight = textViewHeight(textView);
} else {
newInputViewHeight = self.textView.contentSize.height;
}
//10 is the border of the uitoolbar top and bottom
newInputViewHeight += 10;
newInputViewHeight = ceilf(newInputViewHeight);
//newInputViewHeight = MIN(maxInputViewHeight, newInputViewHeight);
// If the new input view height equals the current,
// nothing has to be changed
if (self.textView.bounds.size.height == newInputViewHeight) {
return;
}
// If the new input view height is bigger than the view available, do nothing
if ((self.view.bounds.size.height - self.view.keyboardFrameInView.size.height < newInputViewHeight)) {
return;
}
CGRect inputViewFrame = self.textView.frame;
inputViewFrame.size.height = newInputViewHeight;
self.textView.frame = inputViewFrame;
CGRect toolBarFrame = self.toolBar.frame;
toolBarFrame.size.height = newInputViewHeight +10;
toolBarFrame.origin.y = self.view.keyboardFrameInView.origin.y - toolBarFrame.size.height;
self.toolBar.frame = toolBarFrame;
self.view.keyboardTriggerOffset = self.toolBar.bounds.size.height;
}
static inline CGFloat textViewHeight(UITextView *textView) {
NSTextContainer *textContainer = textView.textContainer;
CGRect textRect =
[textView.layoutManager usedRectForTextContainer:textContainer];
CGFloat textViewHeight = textRect.size.height +
textView.textContainerInset.top + textView.textContainerInset.bottom;
return textViewHeight;
}此代码适用于iOS 7及更高版本..。
发布于 2014-04-08 10:04:20
简单地说:
CGRect textViewFrame = textView.frame;
textViewFrame.size.height = 200;// Or whatever you want, basically do your dynamic calculation before and assign the value here
textView.frame = textViewFrame;如果您希望通过动画方式调整其大小,请将其嵌入到视图动画块中,如下所示:
[UIView animateWithDuration:0.2 animations:^{
CGRect textViewFrame = textView.frame;
textViewFrame.size.height = 200;// Or whatever you want, basically do your dynamic calculation before and assign the value here
textView.frame = textViewFrame;
}];发布于 2014-04-08 10:44:25
使用addKeyboardPanningWithActionHandler:actionHandler方法的DAKeyboardControl。在处理程序块中,添加您的代码,以便在用户处理键盘时动态地更改框架。您可能希望将keyboardTriggerOffset设置为允许当触摸到达textView的帧时进行摇摄。
这里的优点是,即使键盘没有出现/通过平移消失,帧也会相应地调整大小,因为这个块是为键盘帧中的所有更改调用的。
样本代码:
[self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView) {
CGRect textViewFrame = myTextView.frame;
textViewFrame.size.height = keyboardFrameInView.origin.y;
myTextView.frame = textViewFrame;
}];只是一个示例,您可能需要在指定高度的行中进行一些加/减,以正确地调整textView的大小。
https://stackoverflow.com/questions/22933586
复制相似问题