【问题标题】:Recreating the iMessage app [duplicate]重新创建 iMessage 应用程序 [重复]
【发布时间】:2014-03-15 01:55:13
【问题描述】:

我正在做一个消息应用程序,我正在尝试使用 UITextField 和两个按钮(相机和完成)复制键盘顶部的工具栏。当您单击视图中的 UITextField 时,我知道如何将工具栏放在键盘顶部,但我的文本字段在我的工具栏上。单击工具栏上的 UITextField 时,我希望键盘出现在它的下方,然后将工具栏向上推。我还需要它们一起下降,就好像它们是一回事(就像在 iMessage 应用程序中一样)。我查阅了很多教程,但似乎找不到答案。我需要什么代码才能做到这一点?谢谢我不是在问如何将工具栏放在键盘上,我是在问如何触发键盘出现在现有工具栏下并由所述工具栏内的文本视图触发

【问题讨论】:

    标签: objective-c ios7 uitoolbar uikeyboard


    【解决方案1】:

    我写的这段代码复制了iMessage的效果,它使用了一个开源的textview HPGrowingTextView

    - (void)organizeView {
    
    textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(8, 5, 230, 40)];
    textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    textView.minNumberOfLines = 1;
    textView.maxNumberOfLines = 4;
    // you can also set the maximum height in points with maxHeight
    // textView.maxHeight = 200.0f;
    textView.returnKeyType = UIReturnKeySend; //just as an example
    textView.delegate=self;
    textView.font = [UIFont systemFontOfSize:15.0f];
    textView.delegate = self;
    textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    textView.backgroundColor = [UIColor whiteColor];
    textView.placeholder = @"Type your Comment here..";
    
    [textView becomeFirstResponder];
    
    UIImage *rawEntryBackground = [UIImage imageNamed:@""];
    UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    UIImageView *entryImageView = [[UIImageView alloc] initWithImage:entryBackground];
    entryImageView.frame = CGRectMake(5, 0, 248, 40);
    entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    UIImage *rawBackground = [UIImage imageNamed:@"message-strip.png"];
    UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:background];
    imageView.frame = CGRectMake(0, 0, _containerView.frame.size.width, _containerView.frame.size.height);
    imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
     // view hierachy
    [_containerView addSubview:imageView];
    [_containerView addSubview:textView];
    [_containerView addSubview:entryImageView];
    
    UIImage *sendBtnBackground = [[UIImage imageNamed:@"send"] stretchableImageWithLeftCapWidth:13 topCapHeight:0];
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.frame = CGRectMake(_containerView.frame.size.width - 69, 8, 63, 27);
    doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    [doneBtn setImage:sendBtnBackground forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(postButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:doneBtn];
    _containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    }
    
    -(void)resignTextView
    {
    [textView resignFirstResponder];
    }
    
    //Code from Brett Schumann
    -(void) keyboardWillShow:(NSNotification *)note{
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    
    NSInteger kbSizeH = keyboardBounds.size.height;
    // get a rect for the textView frame
    CGRect containerFrame = _containerView.frame;
    containerFrame.origin.y -= kbSizeH-50;
    
    ////reset the table container view height
    CGRect tableContainerFrame=_viewTable.frame;
    tableContainerFrame.size.height=self.view.frame.size.height-(kbSizeH+containerFrame.size.height );
    
    //containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    _containerView.frame = containerFrame;
    _viewTable.frame=tableContainerFrame;
    // commit animations
    [UIView commitAnimations];
    }
    
    -(void) keyboardWillHide:(NSNotification *)note{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    
    // get a rect for the textView frame
    CGRect containerFrame = _containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;
    
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    
    // set views with new info
    _containerView.frame = containerFrame;
    
    // commit animations
    [UIView commitAnimations];
    }
    
    - (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(float)height
    {
    float diff = (growingTextView.frame.size.height - height);
    
    CGRect r = _containerView.frame;
    r.size.height -= diff;
    r.origin.y += diff;
    _containerView.frame = r;
    }
    -(BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView{
    
    if (growingTextView == self->textView) {
        [self performSelector:@selector(postButtonAction:)];
        return NO;
    }
    else
    return YES;
    }
    

    【讨论】:

    • 我为我的无知提前道歉。我将此代码复制并粘贴到我的项目中,然后创建了 HPGrowingTextView 属性,但我仍然有 20 个错误。我还需要做什么才能使这段代码在我的项目中工作。非常感谢
    • 我的大部分错误是“使用未声明的标识符'textView'”和“使用未声明的标识符'containerView'”,然后其他两个是“预期类型”,HPGrowingTextView 为红色。再次感谢
    • 定义 HPGrowingTextView *textView;在.h
    • 我已经为 iMessage 聊天框背后的基本理念发布了此代码。它有点旧的代码。为了便于实施,您可以尝试任何此组件,请关注此link
    • 整个项目代码。所以我可以修复它并将其返回给您而不会出错。
    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    • 2020-01-26
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多