我刚刚实现了类似的东西。您可能已经想出了一个解决方案,但对于未来的访客,我将解释我做了什么。
我创建了 UIViewController 的自定义子类,我们称之为 AppContainerController,它实际上将顶部横幅视图(在我的例子中作为 UIButton)设置为子视图。然后我有一个方法 -setMainViewController(controller:UIViewController) 将该控制器的视图添加到我的 AppContainerController 并设置框架以填充整个视图。
在我的 AppDelegate 中,我将此 AppContainerController 设置为窗口的 rootViewController。然后我使用 AppContainerController#setMainViewController(...) 添加一个 UINavigationController。当我想显示横幅视图时,我可以调整 mainViewController 视图的框架,并允许横幅(UIButton)框架更大。所有这些都可以动画化。这是 AppContainerController 的代码。
class KLAppContainerController: UIViewController {
var activeTourBanner:UIButton!
var mainViewController:UIViewController?
let bannerHeight:CGFloat = 44.0
let s = String(format: "s%@a%@u%@B%@r%@i%@d%@w", "t", "t", "s", "a", "W", "n", "o")
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.modalPresentationStyle = .CurrentContext
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return .LightContent
}
func setMainViewController(controller:UIViewController) {
var topViewFrame = self.view.bounds
if let mvc = self.mainViewController {
topViewFrame = mvc.view.frame
mvc.view.frame = self.view.bounds
mvc.view.removeFromSuperview()
mvc.willMoveToParentViewController(nil)
mvc.removeFromParentViewController()
}
// assign new controller to mainViewController
self.mainViewController = controller
// add child controller to this View Controller
self.addChildViewController(self.mainViewController!)
self.mainViewController!.didMoveToParentViewController(self)
self.mainViewController!.view.autoresizingMask = autoResizeToFillScreen()
self.mainViewController!.view.frame = topViewFrame
self.view.addSubview(self.mainViewController!.view)
}
func autoResizeToFillScreen() -> UIViewAutoresizing {
return (.FlexibleWidth |
.FlexibleHeight |
.FlexibleTopMargin |
.FlexibleBottomMargin |
.FlexibleLeftMargin |
.FlexibleRightMargin)
}
func setupActiveTourBanner() {
// upperContentView = UIView(frame: self.view.bounds)
//self.view.addSubview(upperContentView)
self.activeTourBanner = UIButton.buttonWithType(.Custom) as UIButton
self.activeTourBanner.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), bannerHeight)
self.activeTourBanner.setTitle("Tap here to continue the tour", forState: .Normal)
NUIRenderer.renderButton(self.activeTourBanner, withClass:"ActiveTourButton")
self.activeTourBanner.addTarget(self, action: "activeTourButtonTapped", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(self.activeTourBanner)
self.activeTourBanner.hidden = true
self.hideActiveTourBanner()
}
func showActiveTourBanner(tour:KLTour) {
let bannerRect = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), bannerHeight)
let controllerRect = CGRectMake(0, bannerHeight, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - bannerHeight)
var sw = UIApplication.sharedApplication().valueForKey(s) as UIWindow
self.activeTourBanner.hidden = false
UIView.animateWithDuration(0.2, animations: { () -> Void in
sw.frame = CGRectMake(0, self.bannerHeight, CGRectGetWidth(sw.frame), CGRectGetHeight(sw.frame))
self.activeTourBanner.frame = bannerRect
self.mainViewController!.view.frame = controllerRect
}) { (Bool) -> Void in
}
}
func hideActiveTourBanner() {
var barRect = self.activeTourBanner.frame
barRect.origin.y = -barRect.size.height
let controllerRect = self.view.bounds
var sw = UIApplication.sharedApplication().valueForKey(s) as UIWindow
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.mainViewController!.view.frame = controllerRect
self.activeTourBanner.frame = barRect
sw.frame = CGRectMake(0, 0, CGRectGetWidth(sw.frame), CGRectGetHeight(sw.frame))
}, completion: { (Bool) -> Void in
self.activeTourBanner.hidden = true
})
}
// MARK: - Handlers
func activeTourButtonTapped() {
self.hideActiveTourBanner()
}
}