gpt4 book ai didi

ios - 如何在 swift 中使用@IBDesignable 在文本字段的左侧和右侧插入图像?

转载 作者:行者123 更新时间:2023-11-28 06:03:42 25 4
gpt4 key购买 nike

<分区>

我是初学者,我正在尝试用两个图像制作一个密码文本字段,一个在左侧(锁定图像),另一个在右侧以显示密码,就像这样 enter image description here

enter image description here

但我的上级命令我将它放在文本字段的@IBDesignable 类中,这样主要代码就不会出现在 UI 中。我在这里找到了类似的帖子 how to make designable textfield code class in swift

但似乎代码仅适用于左图或仅适用于右图,我需要它们(锁和眼睛符号)都出现在文本字段中。我试图修改那里的代码,但我无法实现我需要的。真的需要你的帮助,也许你在之前的项目中已经这样做了:)

import UIKit

@IBDesignable


class DesignableTextField: UITextField {

@IBInspectable var rightImage : UIImage? {
didSet {
updateRightView()
}
}

@IBInspectable var rightPadding : CGFloat = 0 {
didSet {
updateRightView()
}
}

@IBInspectable var leftImage : UIImage? {
didSet {
updateView()
}
}

@IBInspectable var leftPadding : CGFloat = 0 {
didSet {
updateView()
}
}

@IBInspectable var cornerRadiusOfField : CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadiusOfField
}
}



func updateView() {
if let image = leftImage {
leftViewMode = .always

// assigning image
let imageView = UIImageView(frame: CGRect(x: leftPadding, y: 0, width: 20, height: 20))
imageView.image = image

var width = leftPadding + 20

if borderStyle == UITextBorderStyle.none || borderStyle == UITextBorderStyle.line {
width += 5
}

let view = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 20)) // has 5 point higher in width in imageView
view.addSubview(imageView)


leftView = view

} else {
// image is nill
leftViewMode = .never
}
}

func updateRightView() {
if let image = rightImage {
rightViewMode = .always

// assigning image
let imageView = UIImageView(frame: CGRect(x: rightPadding, y: 0, width: 20, height: 20))
imageView.image = image

var width = rightPadding - 20

if borderStyle == UITextBorderStyle.none || borderStyle == UITextBorderStyle.line {
width -= 5
}

let view = UIView(frame: CGRect(x: 0, y: 0, width: width, height: 20)) // has 5 point higher in width in imageView
view.addSubview(imageView)


rightView = view

} else {
// image is nill
rightViewMode = .never
}
}



}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com