两个不同的按钮在同一地方

时间:2018-07-06 08:22:35

标签: ios swift constraints

我正在创建一个聊天应用程序。 我有一个发送按钮,当我单击该按钮时,就会发送消息。 我想做的是,当输入文本字段为空白时,发送按钮必须更改为音频记录按钮,并且一旦用户开始键入,音频记录按钮就必须转到发送按钮。我正在尝试在if else函数中设置两个不同的函数,但是我遇到了问题。

lazy var inputContainerView:UIView = {
    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
    containerView.backgroundColor = UIColor(hexString: "#e0e0e0")

    let uploadImageView = UIImageView()
    uploadImageView.isUserInteractionEnabled = true
    uploadImageView.image = UIImage(named: "attachment")
    uploadImageView.translatesAutoresizingMaskIntoConstraints = false
    uploadImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(attachmentButton)))
    containerView.addSubview(uploadImageView)
    // Constraints x,y,width,height
    uploadImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor, constant: 8).isActive = true
    uploadImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    uploadImageView.widthAnchor.constraint(equalToConstant: 30).isActive = true
    uploadImageView.heightAnchor.constraint(equalToConstant: 30).isActive = true

    let recordAudioButton = UIButton(type: .custom)
    let sendbutton = UIButton(type: .system)

    containerView.addSubview(inputTextFiled)
    containerView.addSubview(sendbutton)
    containerView.addSubview(recordAudioButton)
    if inputTextFiled.text == nil {
    sendbutton.setImage(#imageLiteral(resourceName: "sendf"), for: .normal)
    sendbutton.translatesAutoresizingMaskIntoConstraints = false
    sendbutton.addTarget(self, action: #selector(sendButtonPressed), for: .touchUpInside)
    containerView.addSubview(sendbutton)

    // Constraints x,y,width,height
    sendbutton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
    sendbutton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    sendbutton.widthAnchor.constraint(equalToConstant: 50).isActive = true
    sendbutton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true
    } else {
        inputTextFiled.isEnabled = true
        sendbutton.isHidden = true
        sendbutton.isEnabled = false
        recordAudioButton.setImage(#imageLiteral(resourceName: "ic_voice"), for: .normal)
        recordAudioButton.translatesAutoresizingMaskIntoConstraints = false
        recordAudioButton.addTarget(self, action: #selector(recordAudioButtonPressed), for: .touchUpInside)
        inputTextFiled.addSubview(recordAudioButton)
        //Constraints x,y,width,height
        recordAudioButton.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        recordAudioButton.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        recordAudioButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
        recordAudioButton.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

    }
    containerView.addSubview(inputTextFiled)

    //Constraints x,y,width,height

    inputTextFiled.leftAnchor.constraint(equalTo: uploadImageView.rightAnchor, constant:8).isActive = true
    inputTextFiled.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
    inputTextFiled.rightAnchor.constraint(equalTo: sendbutton.leftAnchor).isActive = true
    inputTextFiled.heightAnchor.constraint(equalTo: containerView.heightAnchor,constant: -15).isActive = true

    inputTextFiled.layer.cornerRadius = 16
    inputTextFiled.backgroundColor = UIColor.white

    let seperatorLineView = UIView()
    seperatorLineView.backgroundColor = UIColor.gray
    seperatorLineView.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(seperatorLineView)

    //Constraints X,Y,Width,Height
    seperatorLineView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
    seperatorLineView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
    seperatorLineView.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
    seperatorLineView.heightAnchor.constraint(equalToConstant: 0.5).isActive = true

     return containerView
}()

这里的问题是,当音频记录按钮显示输入文本字段不只显示containerView显示时。

2 个答案:

答案 0 :(得分:0)

您不需要两个按钮。您需要做的就是使用 button.setImage(image:UIImage ?, for:UIControlState)根据textField状态更改按钮的图像。如果是textField.isEmpty,则将图像设置为音频,否则将其设置为发送。在按钮@IBAction内,您可以使用if else语句根据按钮的状态检查要执行的操作。

答案 1 :(得分:0)

因此,经过反复试验,我终于找到了实现目标的方法,但是如果有人发现其他方法,请发布

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let startinglength = textField.text?.count ?? 0
    let lengthToAdd = string.count
    let lengthToReplace = range.length
    let newLength = startinglength + lengthToAdd - lengthToReplace
    if newLength == 0 {
        print("textFiled Empty")
        self.recordAudioButton.setImage(#imageLiteral(resourceName: "ic_voice"), for: .normal)
       self.recordAudioButton.addTarget(self, action: #selector(self.recordAudioButtonPressed), for: .touchUpInside)
       self.recordAudioButton.isEnabled = true
        self.recordAudioButton.isHidden = false
        self.sendbutton.isEnabled = false
        self.sendbutton.isHidden = true
    } else {
        print("textfiled")
        UIView.transition(with: sendbutton, duration: 0.5, options: .transitionFlipFromRight, animations: {
            self.sendbutton.setImage(#imageLiteral(resourceName: "sendf"), for: .normal)
            self.sendbutton.addTarget(self, action: #selector(self.sendButtonPressed), for: .touchUpInside)
            self.recordAudioButton.isEnabled = false
            self.recordAudioButton.isHidden = true
            self.sendbutton.isEnabled = true
            self.sendbutton.isHidden = false
        }, completion: nil)
    }
    return true
}