swift - 如何调整UITextField.leftView的大小?

标签 swift uitextfield

我试图更改调整 UIImageView 的大小,它是我的 UITextField 的 leftView,但没有成功。图像总是巨大的。我希望它与我的 UITextField 具有相同的高度。这是我尝试过的:

结果:

enter image description here

let topContainer: UIView = {
    let view = UIView()
    view.layer.borderWidth = 1.0
    view.layer.borderColor = UIColor.appGrayExtraLightGray.cgColor
    view.layer.cornerRadius = 5
    view.clipsToBounds = true
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

let emailField: UITextField = {
    let view = UITextField()
    view.backgroundColor = UIColor.orange
    view.text = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="365b4f735b575f5a7752524453454576515b575f5a1855595b" rel="noreferrer noopener nofollow">[email protected]</a>"
    view.font = UIFont.systemFont(ofSize: 14)
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

let s = view.safeAreaLayoutGuide

    topContainer.addSubview(emailField)
    emailField.topAnchor.constraint(equalTo: topContainer.topAnchor, constant: spacing).isActive = true
    emailField.leadingAnchor.constraint(equalTo: topContainer.leadingAnchor, constant: spacing).isActive = true
    emailField.trailingAnchor.constraint(equalTo: topContainer.trailingAnchor, constant: -spacing).isActive = true
    emailField.bottomAnchor.constraint(equalTo: topContainer.bottomAnchor, constant: -spacing).isActive = true

    /**Mail and clear button on eitherisde of textField*/
    let mailView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 15, height: 15)) // Chaning the GREct has no effect on the size!!
    mailView.image = UIImage.init(named: "mailGrayFilled")
    emailField.leftView = mailView
    emailField.leftViewMode = .always
    emailField.clearButtonMode = .always

    view.addSubview(topContainer)
    topContainer.topAnchor.constraint(equalTo: s.topAnchor, constant: spacing).isActive = true
    topContainer.leadingAnchor.constraint(equalTo: s.leadingAnchor, constant: spacing).isActive = true
    topContainer.trailingAnchor.constraint(equalTo: s.trailingAnchor, constant: -spacing).isActive = true

最佳答案

当我测试您的代码时,将UIImageView放入容器UIView似乎可以解决问题。这还可以让您调整填充以满足您的需要。

let iconContainer = UIView(frame: CGRect(x: 0, y: 0, width: 25, height: 15))
let mailView = UIImageView(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
mailView.image = UIImage(named: "mailGrayFilled")
mailView.contentMode = .scaleAspectFit
iconContainer.addSubview(mailView)
emailField.leftViewMode = .always
emailField.leftView = iconContainer
emailField.clearButtonMode = .always

关于swift - 如何调整UITextField.leftView的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62089083/

相关文章:

ios - UICollectionViewCell 大小不正确

swift - 如何在显示其他所有内容之前隐藏另一个 Controller 上的点击栏

ios - UITableView 单元格的 editActionsForRowAtIndexPath 中删除旁边的编辑按钮的功能

ios - 如何使用其标签字段使 UITextField 成为第一响应者

ios - 将 inputAccessoryView 与多个 UITextField 和 ViewController 一起使用

ios - 删除特定类型的通知角标(Badge)图标

ios - 当 ViewController 可能实现或不实现协议(protocol)时,在 UIViewController 的 viewDidLoad 上调用协议(protocol)函数

ios - 当我们在 iOS swift 中单击文本字段时,是否可以显示操作表并隐藏键盘?

ios - 如何让键盘直接出现在 UITableViewCell 的文本字段中

ios - 在文本字段外触摸时隐藏键盘