[英]Vertically align UIImageView added as rightView of textfield with the text of skyfloatinglabeltextfield
I have added a checkmarkImageView as rightview of a skyfloatinglabeltextfield as follows:我添加了一个 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!)
It is not aligning vertically centre with the text of the textfield.它没有与文本字段的文本垂直居中对齐。 How do I achieve that?
我如何做到这一点?
change the frame.offsetBy
, I used textfield Border Style
as Plain
更改
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
output输出
Try changing the frame of checkmarkImageView
to:尝试将
checkmarkImageView
的框架更改为:
checkmarkImageView?.frame = CGRect(x: 0.0, y: 0.0, width: 20.0, height: textfield.bounds.height)
And please add a screenshot of the frames you are getting from the UI debugger.并请添加您从 UI 调试器获得的帧的屏幕截图。
Edit:编辑:
checkmarkImageView?.contentMode = .scaleAspectFit
Edit-2:编辑-2:
To get the height of the text, use:要获取文本的高度,请使用:
let textHeight = text.size(withAttributes: [NSAttributedStringKey.font: UIFont.openSansBold(size: 10)]).height
Add the attributes according to your requirement.根据您的要求添加属性。
Let me know if you still face any issues.如果您仍然面临任何问题,请告诉我。
Maybe you can use that function:也许您可以使用该功能:
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
}
Best Regards此致
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.