UIModalPresentationStyle.CurrentContext Swift iOS 7
UIModalPresentationStyle.CurrentContext Swift iOS 7
我想在另一个视图上显示一个视图 - PresentedView
- Background View
使用 iOS 7. 在我的应用程序中,我使用 UITabBArController
,所以在运行时我不知道哪个视图是背景视图(可能是任何标签栏项目)。以下是结构:
UITabBarController
---> TabItem1 - FirstUIViewController
---> TabItem2 - SecondUIViewController
---> TabItem3 - ThirdUIViewController
需要这样的东西:
应用加载时,我在 TabItem1 - FirstUIViewController
。当我单击 TabItem3
时,我希望 ThirdUIViewController
出现在 FirstUIViewController
的顶部,并且 'FirstUIViewController' 应该出现在后台且不启用用户交互。
到目前为止我做了什么:
由于 UIViewControllers
被添加为 Relationship Controllers
以在 `UITabBarController 中显示为 TabBar Item
,我添加了一个从 tabbarcontroller 到 ThridViewController 的 segue。
将此 Segue 的 PresentationStyle 更改为 UIModalPresentationStyle.CurrentContext 并做了以下修改
func `viewDidLoad()` {
super.viewDidLoad()
self.performSegue("identifier", sender: self)
}
没有任何反应,我只看到 'ThridViewController' 有白色背景
我试过手动编码方法:
func `viewDidLoad()` {
super.viewDidLoad()
let overlayController:UIThirdViewController = UIThirdViewController() //This controller has a view added on top of it, which covers top half screen only
overlayController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentViewController(overlayController, animated: true, completion: nil)
}
没有变化。新视图覆盖以前的视图。如果我添加这个 overlayController.view.backgroundColor = UIColor.clearColor()
,我会看到一半屏幕黑色,一半包含我的新视图
问题点:
- 我应该在哪里编写 initialize/call ThirdViewController 的代码以显示在当前视图的顶部?
- 如何解决黑屏问题并使其在 iOS 7 上运行?
我正在使用 Xcode 7 和 运行 iOS7。请帮忙。工作代码片段将不胜感激。不要 post 其他堆栈溢出 post 作为答案,除非代码有效并且您已经自己尝试过。
更新: - 使用这种方法,我得到一个黑屏
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let switchController:UIViewController = SwitchViewController()
self.presentingViewController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentingViewController?.view.backgroundColor = UIColor.clearColor()
self.presentViewController(switchController, animated: true, completion: nil)
return false
}
}
您应该继承 UITabBarController
并将其行为添加到自定义子类中。将 UITabBarController
委托设置为 self
,然后当特定索引被点击而不是让它切换选项卡时,您将以模态方式显示菜单。 UITabBarControllerDelegate
方法将为您提供这样做的机会。
具体使用这个委托方法....
- tabBarController:shouldSelectViewController:
您可以在为哪个选项卡调用此菜单时显示菜单,return 否。这个选项卡只有你一个空白 viewController,因为它永远不会显示给用户。
对于转换本身,我强烈建议使用 UIPresentationController
的子类来将容器视图框架设置为比屏幕尺寸小一点,并添加一个 "dimmingView"。这仅在 iOS8 上可用,所以如果你想要 iOS7,你将需要做更多 hackish 事情。
将其设为自定义容器视图控制器 (Apple Docs)。工作示例代码:
class MyTabVC: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
presentThirdVC()
return false
}
func presentThirdVC() {
let myThirdVC = MyThirdVC.makeVC()
myThirdVC.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.1)
addChildViewController(myThirdVC)
var newFrame = CGRectInset(view.bounds, 0, 50) // hack, do what you want
myThirdVC.view.frame = newFrame
view.addSubview(myThirdVC.view)
didMoveToParentViewController(myThirdVC)
}
}
class MyThirdVC: UIViewController {
class func makeVC() -> MyThirdVC {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("myThirdVC") as! MyThirdVC
}
}
截图:
我想在另一个视图上显示一个视图 - PresentedView
- Background View
使用 iOS 7. 在我的应用程序中,我使用 UITabBArController
,所以在运行时我不知道哪个视图是背景视图(可能是任何标签栏项目)。以下是结构:
UITabBarController
---> TabItem1 - FirstUIViewController
---> TabItem2 - SecondUIViewController
---> TabItem3 - ThirdUIViewController
需要这样的东西:
应用加载时,我在 TabItem1 - FirstUIViewController
。当我单击 TabItem3
时,我希望 ThirdUIViewController
出现在 FirstUIViewController
的顶部,并且 'FirstUIViewController' 应该出现在后台且不启用用户交互。
到目前为止我做了什么:
由于
UIViewControllers
被添加为Relationship Controllers
以在 `UITabBarController 中显示为TabBar Item
,我添加了一个从 tabbarcontroller 到 ThridViewController 的 segue。将此 Segue 的 PresentationStyle 更改为 UIModalPresentationStyle.CurrentContext 并做了以下修改
func `viewDidLoad()` { super.viewDidLoad() self.performSegue("identifier", sender: self) }
没有任何反应,我只看到 'ThridViewController' 有白色背景
我试过手动编码方法:
func `viewDidLoad()` { super.viewDidLoad() let overlayController:UIThirdViewController = UIThirdViewController() //This controller has a view added on top of it, which covers top half screen only overlayController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext self.presentViewController(overlayController, animated: true, completion: nil) }
没有变化。新视图覆盖以前的视图。如果我添加这个 overlayController.view.backgroundColor = UIColor.clearColor()
,我会看到一半屏幕黑色,一半包含我的新视图
问题点:
- 我应该在哪里编写 initialize/call ThirdViewController 的代码以显示在当前视图的顶部?
- 如何解决黑屏问题并使其在 iOS 7 上运行?
我正在使用 Xcode 7 和 运行 iOS7。请帮忙。工作代码片段将不胜感激。不要 post 其他堆栈溢出 post 作为答案,除非代码有效并且您已经自己尝试过。
更新: - 使用这种方法,我得到一个黑屏
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
let switchController:UIViewController = SwitchViewController()
self.presentingViewController?.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentingViewController?.view.backgroundColor = UIColor.clearColor()
self.presentViewController(switchController, animated: true, completion: nil)
return false
}
}
您应该继承 UITabBarController
并将其行为添加到自定义子类中。将 UITabBarController
委托设置为 self
,然后当特定索引被点击而不是让它切换选项卡时,您将以模态方式显示菜单。 UITabBarControllerDelegate
方法将为您提供这样做的机会。
具体使用这个委托方法....
- tabBarController:shouldSelectViewController:
您可以在为哪个选项卡调用此菜单时显示菜单,return 否。这个选项卡只有你一个空白 viewController,因为它永远不会显示给用户。
对于转换本身,我强烈建议使用 UIPresentationController
的子类来将容器视图框架设置为比屏幕尺寸小一点,并添加一个 "dimmingView"。这仅在 iOS8 上可用,所以如果你想要 iOS7,你将需要做更多 hackish 事情。
将其设为自定义容器视图控制器 (Apple Docs)。工作示例代码:
class MyTabVC: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
presentThirdVC()
return false
}
func presentThirdVC() {
let myThirdVC = MyThirdVC.makeVC()
myThirdVC.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.1)
addChildViewController(myThirdVC)
var newFrame = CGRectInset(view.bounds, 0, 50) // hack, do what you want
myThirdVC.view.frame = newFrame
view.addSubview(myThirdVC.view)
didMoveToParentViewController(myThirdVC)
}
}
class MyThirdVC: UIViewController {
class func makeVC() -> MyThirdVC {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("myThirdVC") as! MyThirdVC
}
}
截图: