我有一个UINavigationController作为子视图控制器添加到容器UIViewController类。我需要在导航控制器的视图上方动态显示横幅视图。我调整了导航控制器的视图大小,但是我无法调整导航控制器的子视图控制器视图的大小。
//此代码更新导航控制器的视图,但它不会更新其子视图控制器视图的高度。
self.updateViewYOffset(self.rootNavigationController.view, yOffset: yOffset)
func updateViewYOffset(view: UIView, yOffset: CGFloat) {
var frame = view.frame
frame.origin.y = yOffset
frame.size.height = self.view.frame.size.height - yOffset
view.frame = frame
}
//如果iPhone 5s - 导航控制器的视图高度现在为538(568-30),但其子视图控制器的视图高度仍为568。
答案 0 :(得分:0)
我刚刚实施了这样的事情。您可能已经找到了解决方案,但对于未来的访问者,我将解释我的所作所为。
我创建了一个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()
}
}