gpt4 book ai didi

ios - 如何从 tabbarcontroller View 中删除 subview

转载 作者:行者123 更新时间:2023-11-30 11:53:24 26 4
gpt4 key购买 nike

好的,目前我遇到了一个小问题,我想解决。好的,我有两个 View Controller vc1 和 vc2 嵌入到选项卡栏 Controller 中。现在我在应用程序委托(delegate)中将初始选定项设置为 0 或 vc1。然后,我使用 vc1 的 viewDidLoad 中的函数 addDot() 在选项卡栏 Controller 选项卡栏上添加一个橙色小点。现在我想做的就是当用户单击 vc2 或选项卡栏 Controller 项目 1 时,它会删除 vc1 中添加的橙色点。我在 vc1 中有一个名为“remove dot”的函数,我在 vc2 中调用该函数,但没有任何反应。这是我的一些代码:

 class vc1: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
addDot()
}
let dot = UIView()
func addDot() {

dot.frame = CGRect(x: self.view.frame.width - 97.5, y: self.view.frame.height - 9.5, width: 7, height: 7)
dot.backgroundColor = UIColor(red: 0.949, green: 0.251, blue: 0, alpha: 1.0)
dot.layer.cornerRadius = 3.5
dot.clipsToBounds = true
tabBarController?.view.addSubview(dot)
}
func removerDot () {

dot.removeFromSuperview()
dot.isHidden = true
self.tabBarController?.tabBar.items?[1].title = "Discover"

}
}

和 vc2(选项卡栏项目 1)

  class vc2: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let vc1 = vc1()
vc1.removeDot()
}

}

所以基本上我想做的就是在 tabbarcontroller 选项卡栏上添加一个点,如果它是 1 的选定项,则删除该点。我试图弄清楚,但我不能,请提出你的意见,因为这可能不是一个很难解决的问题。谢谢。

最佳答案

您可以在 UITabBarController 的子类中完成这一切,如下所示:

class MyTabBarController: UITabBarController {
let dot = UIView()

override func viewDidLoad() {
super.viewDidLoad()

// Setup the initial attributes of the dot here.
dot.frame = CGRect(x: 50, y: 50, width: 7, height: 7)
dot.backgroundColor = UIColor(red: 0.949, green: 0.251, blue: 0, alpha: 1.0)
dot.layer.cornerRadius = 3.5
dot.clipsToBounds = true
self.view.addSubview(dot)
}

override var selectedViewController: UIViewController? {
get {
return super.selectedViewController
}
set {
super.selectedViewController = newValue
// Determine which controller has been selected and adjust the dot accordingly.
}
}
}

当选定的 View Controller 发生更改时,您将需要确定在哪里放置代码,但您已经掌握了基础知识。

现在已经说过,这可能不是整体上最好的方法,因为您将不得不手动处理框架等。如果可以的话,您最好为未选择和选定的状态使用单独的图像它们中已有的点,或者您可以尝试更改文本标签。

关于ios - 如何从 tabbarcontroller View 中删除 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48104643/

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