我添加了一个checkmarkImageView作为skyfloatinglabeltextfield的rightview,如下所示:

let checkmark = UIImage(named: "checkmark")
checkmarkImageView = UIImageView(image: checkmark)
checkmarkImageView?.frame = CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0)
textfield?.rightView = checkmarkImageView
textfield?.rightViewMode = .always
editView.addSubview(textfield!)

它未与文本字段的文本垂直居中对齐。我该如何实现?
ios - 将添加为textfield的rightView的UIImageView与skyfloatinglabeltextfield的文本垂直对齐-LMLPHP

最佳答案

更改frame.offsetBy,我使用文本字段Border Style作为Plain

    let rightVie = UIView()
    rightVie.backgroundColor = UIColor.clear

   checkmarkImageView = UIImageView(image: UIImage(named: "checkmark")) // use your imageView
  //  checkmarkImageView?.frame = CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0)
   checkmarkImageView.frame = checkmarkImageView.frame.offsetBy(dx: 0, dy: -1 + textfield.titleHeight()/2)
    rightVie.frame = checkmarkImageView.frame
    rightVie.addSubview(checkmarkImageView)
    //use your textfield name
    textfield?.rightView = rightVie
    textfield?.rightViewMode = .always

输出

ios - 将添加为textfield的rightView的UIImageView与skyfloatinglabeltextfield的文本垂直对齐-LMLPHP

关于ios - 将添加为textfield的rightView的UIImageView与skyfloatinglabeltextfield的文本垂直对齐,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50405272/

10-10 21:05