ios 如何在点击自定义UITextField时隐藏UIView()(以编程方式)?

jgwigjjp  于 2023-02-26  发布在  iOS
关注(0)|答案(1)|浏览(103)

我遇到了一个问题,我不明白,也无法解决。当textField被激活时,键盘上的数字按钮被按下时会显示键盘,我的stackView位于屏幕中间,应该隐藏,我的tableView应该上升,从headView缩进36点。当我删除textField中的文本时,所有的东西都应该回到屏幕截图中的标准视图。2我使用了UIKit编程,没有StoryBoardSnapKit作为约束。3这里我应该使用什么方法?4 textField delegate还是UIView.animate {}还是smth。5你能帮我吗?
我的UITextField是greenColor,我将它设置为类似于视图,只是为了示例和演示。

final class HeaderView: UIView {
   
   // There are contains UIImageView & UITextField
   // UIImageView has yellow color in screenShot
   // UITextField has green color in screenShot
   ..........
}

final class LeftView: UIView {
  
  // Contains textLabel and IconImg
  ...........
}

final class RightView: UIView {
  
  // Contains textLabel and IconImg
  ...........
}

并在我的UIViewController中调用它们:

final class ViewController: UIViewController {

        private let headerLabel: UILabel = {
            let label = UILabel()
            label.font = .systemFont(ofSize: 17, weight: .semibold)
            label.text = "Hello World!"
            label.textColor = .black
            return label
        }()
 
        // Calling my custom views
        let headView = HeaderView()
        let leftView = LeftView()
        let rightView = RightView()
        
        let stackView: UIStackView = {
           let stackView = UIStackView()
           stackView.axis = .horizontal
           stackView.alignment = .center
           stackView.distribution = .equalSpacing
           return stackView
        }()

        // After that, I created UITableView
        let tableView: UITableView = {
            let tableView = UITableView(frame: .zero)
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
            return tableView
        }()

        // ViewDidLoad
        override func viewDidLoad() {
           super.viewDidLoad()
           self.view.backgroundColor = .systemBackground
           
           tableView.delegate = self
           tableView.dataSource = self

           self.setupUI()
        }

        func setupUI() {
           self.setSubviews()
           self.setConstraints()
        }

        func setSubviews() {
           self.view.addSubview(headerLabel)
           self.view.addSubview(headView)
           self.stackView.addArrangedSubview(leftView)
           self.stackView.addArrangedSubview(rightView)
           self.view.addSubview(stackView)
           self.view.addSubview(tableView)
        }

        private func setConstraints() {
    
           headerLabel.snp.makeConstraints { make in
              make.top.equalTo(self.view.safeAreaLayoutGuide).offset(30)
              make.centerX.equalTo(self.view.safeAreaLayoutGuide)
           }
    
           headView.snp.makeConstraints { make in
              make.top.equalTo(self.headerLabel.snp.bottom).offset(24)
              make.leading.equalTo(self.view.safeAreaLayoutGuide).offset(16)
              make.trailing.equalTo(self.view.safeAreaLayoutGuide).offset(-16)
              make.height.equalTo(56)
          }
    
          stackView.snp.makeConstraints { make in
              make.top.equalTo(self.headView.snp.bottom).offset(16)
              make.leading.trailing.equalToSuperview().inset(16)
          }
    
          leftView.snp.makeConstraints { make in
              make.top.equalTo(self.stackView.snp.top)
              make.leading.equalTo(self.stackView.snp.leading)
              make.bottom.equalTo(self.stackView.snp.bottom)
              make.size.equalTo(CGSize(width: 163, height: 88))
          }
    
          rightView.snp.makeConstraints { make in
              make.top.equalTo(self.stackView.snp.top)
              make.trailing.equalTo(self.stackView.snp.trailing)
              make.bottom.equalTo(self.stackView.snp.bottom)
              make.size.equalTo(CGSize(width: 163, height: 88))
          }
    
          tableView.snp.makeConstraints { make in
             make.top.equalTo(self.stackView.snp.bottom).offset(36)
             make.leading.trailing.equalTo(self.view.safeAreaLayoutGuide)
             make.bottom.equalTo(self.view.safeAreaLayoutGuide)
          }
      }
}

extension UIViewController: UITableViewDelegate, UITableViewDataSource {

     public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
     }

     public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
       cell.textLabel?.text = "Hello"
       return cell
    }
}

[![在此输入图像说明][1]][1]

qxgroojn

qxgroojn1#

要在文本字段中键入文本或数字时隐藏UIView,必须像向UIButton添加目标一样向UIText字段添加目标,但是,对于UIText字段,必须更改UIControl.Event(. editingChanged)。
例如:

let textField = UITextFiled()

override func viewDidLoad() {

  // Add all the required stuffs of UItextfield and add the following code

       textField(self, action: #selector(txtValueChanged(_ :)), for: .editingChanged)

}

@objc func txtValueChanged(_ sender: UITextField) {

  // You have to hide LeftView,RightView . So, add this code

      leftView.isHidden = true
      rightView.isHidden = true

}

这段代码将在UITextfield中键入字符时隐藏视图。

相关问题