是否可以在Swift中为父VC的视图设置动画?
我有一个带有UIView的root / master VC,我将其作为一种UITabBarController使用,因此我的4个主要VC的其余部分都是root用户。
在一些子级VC中,我的子视图应该占据整个屏幕,而不会看到来自根VC的自定义标签栏(UIView),但它仍然浮在上面。
每当我打开全屏子视图时,我想让它通过Y轴从屏幕上滑落,但是我似乎无法访问或操作根VC的属性,因为它在运行时返回nil。 /强>
这里是自定义标签栏根VC,因此您可以了解代码的结构:
class RootVC: UIViewController {
//This is where we pull all of our content from other VCs
//when a tab bar button is selected
@IBOutlet weak var contentView: UIView!
//The custom tab bar itself with an array of button outlets
@IBOutlet public weak var customTabBarContainer: UIView!
@IBOutlet var tabBarButtons: [UIButton]!
//4 main view VCs that are reflected in the tab bar
public var mapVC: UIViewController!
public var favoritesVC: UIViewController!
public var chatVC: UIViewController!
public var profileVC: UIViewController!
//Array for the VCs above
public var viewControllers: [UIViewController]!
//Index of the selected button determend by their tags
public var selectedIndex: Int = 0
@IBOutlet weak var loadingLogo: UIImageView!
override public func viewDidLoad() {
//Populating viewControllers array with
//initiated VCs in Main storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
mapVC = storyboard.instantiateViewController(withIdentifier: "MapVC")
favoritesVC = storyboard.instantiateViewController(withIdentifier: "FavoritesVC")
chatVC = storyboard.instantiateViewController(withIdentifier: "ChatVC")
profileVC = storyboard.instantiateViewController(withIdentifier: "ProfileVC")
viewControllers = [mapVC, favoritesVC, chatVC, profileVC]
//Custom tab bar + buttons visual properties
customTabBarContainer.layer.cornerRadius = customTabBarContainer.frame.height / 2
customTabBarContainer.layer.shadowColor = UIColor.darkGray.cgColor
customTabBarContainer.layer.shadowOffset = CGSize.zero
customTabBarContainer.layer.shadowRadius = 10
customTabBarContainer.layer.shadowOpacity = 0.9
tabBarButtons[0].imageView?.contentMode = .scaleAspectFit
tabBarButtons[1].imageView?.contentMode = .scaleAspectFit
tabBarButtons[2].imageView?.contentMode = .scaleAspectFit
tabBarButtons[3].imageView?.contentMode = .scaleAspectFit
}
override public func viewDidAppear(_ animated: Bool) {
loadingLogo.popOut()
//Loads the initial VC
contentView.addSubview(mapVC.view)
mapVC.view.frame = self.view.frame
mapVC.didMove(toParentViewController: self)
customTabBarContainer.isHidden = false
//Selects the inital home button
tabBarButtons[0].isSelected = true
}
@IBAction func didTabButton(_ sender: UIButton) {
//Keeps a track of which bar button is selected
let previousIndex = selectedIndex
selectedIndex = sender.tag
//Deselects the previous bar button
tabBarButtons[previousIndex].isSelected = false
//Removes the previous VC
let previousVC = viewControllers[previousIndex]
previousVC.view.removeFromSuperview()
previousVC.removeFromParentViewController()
print("switced to \(viewControllers[selectedIndex])")
//Selects the tapped bar button
tabBarButtons[selectedIndex].isSelected = true
tabBarButtons[selectedIndex].popIn()
//Brings up the selected VC
let nextVC = viewControllers[selectedIndex]
contentView.addSubview(nextVC.view)
nextVC.view.frame = self.view.frame
nextVC.didMove(toParentViewController: self)
}
}
以下是我试图用来操纵MapVC子项的customTabBarContainer的代码:
UIView.animate(withDuration: 0.4, animations: {
let root = self.parent?.parent as! RootVC
root.customTabBarContainer.frame.origin.y -= root.customTabBarContainer.frame.height
}, completion: nil)
答案 0 :(得分:0)
为什么你试图访问父母的父母呢?
self.parent?.parent as RootVC
假设您正在使用像这样的扩展来查找您的parentVC:
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if parentResponder is UIViewController {
return parentResponder as! UIViewController!
}
}
return nil
}
}
您应该可以通过
访问父级let root = self.parentViewController as! RootVC
答案 1 :(得分:0)
我找到了答案,以防万一其他人遇到类似的问题。它不会把你带到VC的直接父母,而是带到最遥远的祖先,这解决了我在这种情况下的特殊问题。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let rootVC = appDelegate.window?.rootViewController as! RootVC
rootVC.customTabBarContainer.isHidden = true