1

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

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!)

它没有与文本字段的文本垂直居中对齐。我该如何做到这一点? 它没有与文本字段的文本垂直居中对齐。

4

3 回答 3

2

更改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

输出

在此处输入图像描述

于 2018-05-18T07:11:36.090 回答
1

尝试将框架更改checkmarkImageView为:

checkmarkImageView?.frame = CGRect(x: 0.0, y: 0.0, width: 20.0, height: textfield.bounds.height)

请添加您从 UI 调试器获取的帧的屏幕截图。

编辑:

checkmarkImageView?.contentMode = .scaleAspectFit

编辑2:

要获取文本的高度,请使用:

let textHeight = text.size(withAttributes: [NSAttributedStringKey.font: UIFont.openSansBold(size: 10)]).height

根据您的要求添加属性。

如果您仍然遇到任何问题,请告诉我。

于 2018-05-18T06:55:39.980 回答
0

也许您可以使用该功能:

func setupNavUI() {
    let addressTextField = UITextField(frame: CGRect(x: 0.0, y: 0.0, width: self.navigationItem.titleView?.frame.width ?? 100.0, height: 50.0))
    addressTextField.rightViewMode = .always
    let icon = FAKFontAwesome.sortDownIcon(withSize: 20.0)
    icon?.setAttributes([NSAttributedString.Key.foregroundColor : UIColor.lightGray])
    let image = icon?.image(with: CGSize(width: 20.0, height: 20.0))
    let imageView = UIImageView(frame: CGRect(x: 0.0, y: 0.0, width: 20.0, height: 20.0))
    imageView.image = image
    let rightView = UIView(frame: CGRect(x: 0, y: 0, width: 20.0, height: 26.0))
    rightView.addSubview(imageView)
    addressTextField.rightView = rightView
    addressTextField.placeholder = "Buscar una dirección"
    self.navigationItem.titleView = addressTextField
}

此致

于 2020-06-08T16:50:49.273 回答