如何在xcode中添加图像到占位符?

时间:2022-04-02 22:51:26

Currently working in Swift with Xcode but I want to add, for example, an image icon into my placeholder position for a text field. Many modern UI designs feature this on their log in/log out pages. How would I be able to implement something like this?

目前正在使用Xcode在Swift中工作,但我想将一个图像图标添加到文本字段的占位符位置。许多现代UI设计在其登录/注销页面上具有此功能。我怎么能实现这样的东西?

2 个解决方案

#1


0  

You can add a UIButton using Interface Builder and have both an image and text into one object.

您可以使用Interface Builder添加UIButton,并将图像和文本都放在一个对象中。

Simply drag and drop a UIButton from the object library panel to a UIViewController. Check the Attributes Inspector and set the Image property to an image you've added to the project either using the Assets Catalog or from an image you've added to the project library. Then set the Title property to the text you wish to display. You can then select Image in the Edge dropdown property and adjust the position of the image relative to the text.

只需将UIButton从对象库面板拖放到UIViewController即可。检查“属性”检查器,并使用“资产目录”或从已添加到项目库的图像将“图像”属性设置为已添加到项目中的图像。然后将Title属性设置为您要显示的文本。然后,您可以在“边缘”下拉属性中选择“图像”,并调整图像相对于文本的位置。

#2


0  

you can check this code for your text field image icon

您可以检查此代码中的文本字段图像图标

 @IBOutlet weak var name: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    name.leftViewMode = .always
    let nameimage = UIImageView()
    nameimage.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
    let anam = #imageLiteral(resourceName: "m")
    nameimage.image = anam
    name.leftView = nameimage
}

#1


0  

You can add a UIButton using Interface Builder and have both an image and text into one object.

您可以使用Interface Builder添加UIButton,并将图像和文本都放在一个对象中。

Simply drag and drop a UIButton from the object library panel to a UIViewController. Check the Attributes Inspector and set the Image property to an image you've added to the project either using the Assets Catalog or from an image you've added to the project library. Then set the Title property to the text you wish to display. You can then select Image in the Edge dropdown property and adjust the position of the image relative to the text.

只需将UIButton从对象库面板拖放到UIViewController即可。检查“属性”检查器,并使用“资产目录”或从已添加到项目库的图像将“图像”属性设置为已添加到项目中的图像。然后将Title属性设置为您要显示的文本。然后,您可以在“边缘”下拉属性中选择“图像”,并调整图像相对于文本的位置。

#2


0  

you can check this code for your text field image icon

您可以检查此代码中的文本字段图像图标

 @IBOutlet weak var name: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    name.leftViewMode = .always
    let nameimage = UIImageView()
    nameimage.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
    let anam = #imageLiteral(resourceName: "m")
    nameimage.image = anam
    name.leftView = nameimage
}