gpt4 book ai didi

ios - 如何以编程方式将 UIImageView 添加到 UIStackView( Storyboard)

转载 作者:行者123 更新时间:2023-11-28 06:05:53 33 4
gpt4 key购买 nike

我主要在 Storyboard 中设计布局。但是现在我想删除/隐藏一个 UIButton( Storyboard),然后以编程方式添加 UIImageView。

我设法以编程方式隐藏了 UIButton,但很难在 UIStackView 中正确显示 UIImageView。

UIStackView 已经包含了一个在 Storyboard 中设计的 UIView。我需要以编程方式将 UIImageView 添加到此水平 UIStackView 中 UIView 的右侧。

有什么建议吗?

import UIKit
import Firebase
import Photos
import Kingfisher

class editProfileVC: UIViewController {

var userArray = [User]()

// StackView outlets needed to set background color
@IBOutlet weak var backgroundStackView: UIStackView!

// Normal outlets
@IBOutlet weak var firstName: UITextField! // required
@IBOutlet weak var lastName: UITextField! // required - type of hunt
@IBOutlet weak var email: UITextField! // required - weapon name
@IBOutlet weak var city: UITextField! // optional (eg. 1)
@IBOutlet weak var country: UITextField! // optional (eg. 2)
@IBOutlet weak var primaryHunt: UITextField! // optional short description of the hunt

@IBOutlet weak var saveBtn: UIButton!
@IBOutlet weak var cancelBtn: UIButton!

@IBOutlet weak var addImageBtn: UIButton!
@IBOutlet weak var addImageStackView: UIStackView!

override func viewDidLoad() {
super.viewDidLoad()

if Auth.auth().currentUser == nil {
let authVC = self.storyboard?.instantiateViewController(withIdentifier: "authVC") as? authVC
self.present(authVC!, animated: true, completion: nil)
}

pinBackground(backgroundView, to: backgroundStackView)

// Get userdata from Firebase
let userId = (Auth.auth().currentUser?.uid)!

DataService.instance.getUserdata(userId: userId) { (returnedUserArray) in

if returnedUserArray.isEmpty == false {

// if user exist populate textfields with information from database
self.firstName.text = returnedUserArray[0].firstName
self.lastName.text = returnedUserArray[0].lastName
self.city.text = returnedUserArray[0].city
self.country.text = returnedUserArray[0].country
self.primaryHunt.text = returnedUserArray[0].defaultHunt

if returnedUserArray[0].profileImageURL.isEmpty == false {
// hide addImageBtn
self.addImageBtn.isHidden = true

// show profile image
let imageView = UIImageView()
imageView.kf.setImage(with: URL.init(string:returnedUserArray[0].profileImageURL))
self.addImageStackView.addSubview(imageView)

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 50).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 50).isActive = true
}
}
}
}
}

UIStackView 是绿色栏下方的顶部部分。灰色个人资料图标中的 addImageBtn 带有文本“添加个人资料图像”。

enter image description here

最佳答案

要将任何 View 添加到 UIStackView,您可以执行以下操作:

stackView.addArrangedSubview(view)

如果你想在某个位置添加 View ,那么这样做:

stackView.insertArrangedSubview(view, at: 1) // Be careful you get the index correct.

如果您需要删除您执行此操作的 View :

stackView.removeArrangedSubview(view)
view.removeFromSuperview() // This is needed because the view still remains a sub view of the UIStackView.

它的外观取决于您如何设置堆栈 View 。

关于ios - 如何以编程方式将 UIImageView 添加到 UIStackView( Storyboard),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48252815/

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