Ios 以编程方式编辑视图
我目前正在尝试制作一个简单的应用程序,它将根据用户的输入显示少量的内容。目前,我的屏幕上有一个堆栈视图,其中有一个UIView。我试图使它,一旦按下一个按钮,UIView将消失,然后当另一个按钮被按下时,一个新的将出现。按钮可以一直按下,更多视图将被放入堆栈视图中 这是我目前拥有的,它不起作用,在搜索了一段时间的答案后,我不知道如何让它起作用:Ios 以编程方式编辑视图,ios,swift,xcode,uiviewcontroller,uistackview,Ios,Swift,Xcode,Uiviewcontroller,Uistackview,我目前正在尝试制作一个简单的应用程序,它将根据用户的输入显示少量的内容。目前,我的屏幕上有一个堆栈视图,其中有一个UIView。我试图使它,一旦按下一个按钮,UIView将消失,然后当另一个按钮被按下时,一个新的将出现。按钮可以一直按下,更多视图将被放入堆栈视图中 这是我目前拥有的,它不起作用,在搜索了一段时间的答案后,我不知道如何让它起作用: import UIKit class ViewController: UIViewController { @IBAction fu
import UIKit
class ViewController: UIViewController {
@IBAction func deleteButtonPressed(_ button: PushButton) {
@IBOutlet weak var containerStackView: UIStackView!
containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
}
func removeIcons() -> UIView {
let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
return newView
}
}
有没有办法引用堆栈视图,然后对其进行更改
--编辑
我现在有了这个代码:
class ViewController: UIViewController {
@IBOutlet var containerStackView: UIStackView!
@IBAction func deleteButtonPressed(_ sender: Any) {
let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
newView.backgroundColor = UIColor.black
containerStackView = UIStackView(arrangedSubviews: [newView])
}
}
这似乎完美地代表了堆栈视图,但是,当我按下按钮时,堆栈视图似乎没有发生任何变化。理论上,当前存在的UIView应该被创建为“newView”的黑色小正方形所取代。虽然这似乎没有发生-有什么明显的我错过了
对于这些问题,我深表歉意,我是swift和xcode的新手。在类范围外声明函数的出口
class ViewController: UIViewController {
@IBOutlet weak var containerStackView: UIStackView! // here
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func deleteButtonPressed(_ button: PushButton) {
containerStackView.subviews.forEach { $0.removeFromsuperView() }
}
@IBAction func daddButtonPressed(_ button: UIButton) {
containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
}
func removeIcons() -> UIView {
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
return newView
}
}
在类作用域外部声明函数外部的出口
class ViewController: UIViewController {
@IBOutlet weak var containerStackView: UIStackView! // here
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func deleteButtonPressed(_ button: PushButton) {
containerStackView.subviews.forEach { $0.removeFromsuperView() }
}
@IBAction func daddButtonPressed(_ button: UIButton) {
containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
}
func removeIcons() -> UIView {
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
return newView
}
}
试试这个:
class ViewController: UIViewController {
//...
@IBOutlet weak var containerStackView: UIStackView!
//...
@IBAction func deleteButtonPressed(_ button: PushButton) {
let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
containerStackView.addArrangedSubview(newView)
}
}
试试这个:
class ViewController: UIViewController {
//...
@IBOutlet weak var containerStackView: UIStackView!
//...
@IBAction func deleteButtonPressed(_ button: PushButton) {
let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
containerStackView.addArrangedSubview(newView)
}
}
首先,您的门店应位于顶层:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var containerStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func deleteButtonPressed(_ button: PushButton) {
containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
}
func removeIcons() -> UIView {
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
return newView
}
}
首先,您的门店应位于顶层:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var containerStackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func deleteButtonPressed(_ button: PushButton) {
containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
}
func removeIcons() -> UIView {
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
return newView
}
}