UINavigationBar content margins in UIPresentationController subclass
UINavigationBar content margins in UIPresentationController subclass
- Subject: UINavigationBar content margins in UIPresentationController subclass
- From: Daniel Stenmark <email@hidden>
- Date: Mon, 07 Nov 2016 20:42:49 +0000
- Thread-topic: UINavigationBar content margins in UIPresentationController subclass
I have a UIPresentationController subclass with a UINavigationBar embedded in the container view. However, when setting the rightBarButtonItem, the spacing I usually expect from the item to the screen isn't there.
http://imgur.com/LHcqhyd<http://imgur.com/a/610al>
Anyone have an idea where I might be going wrong here? The relevant code for the presentation controller is as follows:
@objc public protocol SheetPresentationControllerDelegate : class {
func sheetPresentationControllerWillDismiss(_ sheetPresentationController: SheetPresentationController)
}
open class SheetPresentationController: UIPresentationController {
fileprivate let dimmedView: UIView = {
let result = UIView()
result.backgroundColor = .black
result.alpha = 0
result.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return result
}()
fileprivate let topView: UIView = {
let result = UIView()
result.backgroundColor = .white
return result
}()
fileprivate let navigationBar: UINavigationBar = {
let result = UINavigationBar()
result.backgroundColor = .white
result.items = [UINavigationItem()]
return result
}()
open var title: String? {
get {
return self.navigationBar.topItem?.title
}
set {
self.navigationBar.topItem?.title = newValue
}
}
open var titleView: UIView? {
get {
return self.navigationBar.topItem?.titleView
}
set {
self.navigationBar.topItem?.titleView = newValue
}
}
open weak var sheetDelegate: SheetPresentationControllerDelegate?
fileprivate dynamic func dismiss(_ sender: NSObject) {
self.presentingViewController.dismiss(animated: true, completion: nil)
}
override open func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()
guard let containerView = self.containerView else {
return
}
containerView.addSubview(self.dimmedView)
self.dimmedView.frame = CGRect(origin: .zero, size: containerView.bounds.size)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismiss))
self.dimmedView.addGestureRecognizer(gestureRecognizer)
self.presentedViewController.transitionCoordinator?.animate( alongsideTransition: { (context) in
self.dimmedView.alpha = 0.5
}, completion: nil)
containerView.addSubview(self.topView)
containerView.addSubview(self.navigationBar)
self.navigationBar.topItem?.rightBarButtonItems = [UIBarButtonItem(title: NSLocalizedString("Done", comment: ""), style: .done, target: self, action: #selector(dismiss))]
}
override open func dismissalTransitionWillBegin() {
super.dismissalTransitionWillBegin()
self.sheetDelegate?.sheetPresentationControllerWillDismiss(self)
self.presentedViewController.transitionCoordinator?.animate( alongsideTransition: { (context) in
self.dimmedView.alpha = 0
}, completion: nil)
}
override open var frameOfPresentedViewInContainerView : CGRect {
return CGRect(x: 0, y: self.navigationBar.frame.origin.y + self.navigationBar.intrinsicContentSize.height, width: self.containerView?.bounds.width ?? 0, height: self.presentedViewController.preferredContentSize.height)
}
override open func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
self.topView.frame = CGRect(x: 0, y: 0, width: self.containerView?.bounds.width ?? 0, height: self.presentingViewController.topLayoutGuide.length)
self.navigationBar.frame = CGRect(x: 0, y: self.topView.frame.origin.y + self.topView.bounds.height, width: self.containerView?.bounds.width ?? 0, height: self.navigationBar.intrinsicContentSize.height)
self.presentedView?.frame = self.frameOfPresentedViewInContainerView
}
}
Dan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden