在JSQMessageViewController的inputToolbar上添加视图/按钮

时间:2017-07-12 10:21:19

标签: ios objective-c iphone jsqmessagesviewcontroller

我正在尝试在JSQMessageViewController的inputToolbar中添加additionalButton,但问题是每当inputToolbar辞职或成为firstResponder时,inputToolbar的框架contentView reFrames本身和additionalButton出现在textview上。任何人都可以帮助我在这种情况下做什么,additionalButton和inputToolbar的textview不会相互重叠。

这是以下代码:

@viewDidAppear

CGRect leftBtnFrame = self.inputToolbar.contentView.leftBarButtonContainerView.frame;
btnRabbit = [[UIButton alloc]initWithFrame:CGRectMake(leftBtnFrame.origin.x + leftBtnFrame.size.width +5 , leftBtnFrame.origin.y, leftBtnFrame.size.width, leftBtnFrame.size.height)];
[btnRabbit addTarget:self action:@selector(btnRabbitPressed) forControlEvents:UIControlEventTouchUpInside];
[btnRabbit setImage:[UIImage imageNamed:@"donkey_icon"] forState:UIControlStateNormal];



textViewFrame =  self.inputToolbar.contentView.textView.frame;
textViewFrame.origin.x = textViewFrame.origin.x +btnRabbit.frame.origin.x - self.inputToolbar.contentView.leftContentPadding;
textViewFrame.origin.y = textViewFrame.origin.y;
textViewFrame.size.height = textViewFrame.size.height;
textViewFrame.size.width = textViewFrame.size.width - btnRabbit.frame.origin.x;

[self setToolBar];

@setToolBar

 [UIView animateWithDuration:0.2 animations:^{
  [self.inputToolbar.contentView addSubview:btnRabbit];
} completion:^(BOOL finished) {
    [self.inputToolbar.contentView.textView setFrame:textViewFrame];
}];

我在以下键盘通知观察者委托

中调用setToolBar
-(void)keyboardWillShow:(NSNotification *)notification {
    [self setToolBar ];
}

first time[![][1[![]] 2] 2] 3

2 个答案:

答案 0 :(得分:0)

以下是我实施的方式

- (void)setupRightBarButtonItems {
    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.inputToolbar.contentView.rightBarButtonItem = sendButton;
    sendButton.translatesAutoresizingMaskIntoConstraints = NO;
    [sendButton setImage:[UIImage imageNamed:@"sendButtonActive"] forState:UIControlStateNormal];
    [sendButton setImage:[UIImage imageNamed:@"sendButton"] forState:UIControlStateDisabled];
    sendButton.contentMode = UIViewContentModeRight;
    sendButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [sendButton setImageEdgeInsets:UIEdgeInsetsMake(0, -4, 0, 0)];
    [sendButton removeConstraints:sendButton.constraints];

    //Autolayout
    [sendButton setTrailingConstant:0];
    [sendButton setTopConstant:0];
    [sendButton setBottomConstant:0];
    [sendButton setWidthConstant:54];
    self.sendButton = sendButton;

    self.inputToolbar.contentView.rightBarButtonItemWidth = 114;

    self.videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.videoButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.inputToolbar.contentView.rightBarButtonContainerView addSubview:self.videoButton];
    [self.videoButton setImage:[UIImage imageNamed:@"videoButton"] forState:UIControlStateNormal];
    self.videoButton.contentMode = UIViewContentModeRight;
    self.videoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

    [self.videoButton addTarget:self
                         action:@selector(didTappedVideoButton:)
               forControlEvents:UIControlEventTouchUpInside];

    //Autolayout
    [self.videoButton setHorizontalSpacing:0 fromView:self.sendButton];
    [self.videoButton setTopConstant:0];
    [self.videoButton setBottomConstant:0];
    [self.videoButton setWidthConstant:30];
    //

    self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.photoButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.inputToolbar.contentView.rightBarButtonContainerView addSubview:self.photoButton];
    [self.photoButton setImage:[UIImage imageNamed:@"photoButton"] forState:UIControlStateNormal];
    self.photoButton.contentMode = UIViewContentModeRight;
    self.photoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [self.photoButton addTarget:self
                         action:@selector(didTappedPhotoButton:)
               forControlEvents:UIControlEventTouchUpInside];

    //Autolayout
    [self.photoButton setHorizontalSpacing:0 fromView:self.videoButton];
    [self.photoButton setTopConstant:0];
    [self.photoButton setBottomConstant:0];
    [self.photoButton setWidthConstant:30];

    self.inputToolbar.contentView.rightContentPadding = 0;
}

结果:

enter image description here

答案 1 :(得分:0)

设置按钮

let btnRabbit = UIButton()
btnRabbit.setImage(#imageLiteral(resourceName: "Rabbit"), for: .normal)

将其放在工具栏中:

self.inputToolbar?.contentView?.leftBarButtonItem = btnRabbit

将它放在您想要的位置并正确调整文本输入。