我正在尝试创建一个自定义的简短菜单,该菜单从屏幕底部向上滑动并停留在底部(例如iOS共享表)。我很难弄清楚该怎么做。我尝试将视图控制器呈现为模式并设置首选的内容大小,但仍将其呈现为全屏显示。如何呈现类似模式的简短叠加层?
答案 0 :(得分:1)
您可以使用UIPresentationController和UIViewControllerTransitioningDelegate。
这里是几行代码的起点:
UIViewControllerTransitioningDelegate
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
decimal accNo = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
var item = db.UserAccount.Where(a => a.AccountNo == accNo).FirstOrDefault();
txtAccountNo.Text = item.AccountNo.ToString();
txtFirstName.Text = item.FirstName;
txtLastName.Text = item.LastName;
txtAddressUpNew.Text = item.Address.ToString();
}
UIPresentationController
class OverlayTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return OverlayPresentationController(presentedViewController:presented, presenting:presenting)
}
}
如何命名
class OverlayPresentationController: UIPresentationController {
private let dimmedBackgroundView = UIView()
private let height: CGFloat = 200.0
override init(presentedViewController: UIViewController, presenting presentingViewController: UIViewController?) {
super.init(presentedViewController: presentedViewController, presenting: presentingViewController)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped))
self.dimmedBackgroundView.addGestureRecognizer(tapGestureRecognizer)
}
override var frameOfPresentedViewInContainerView: CGRect {
var frame = CGRect.zero
if let containerBounds = containerView?.bounds {
frame = CGRect(x: 0,
y: containerBounds.height - height,
width: containerBounds.width,
height: height)
}
return frame
}
override func presentationTransitionWillBegin() {
if let containerView = self.containerView, let coordinator = presentingViewController.transitionCoordinator {
containerView.addSubview(self.dimmedBackgroundView)
self.dimmedBackgroundView.backgroundColor = .black
self.dimmedBackgroundView.frame = containerView.bounds
self.dimmedBackgroundView.alpha = 0
coordinator.animate(alongsideTransition: { _ in
self.dimmedBackgroundView.alpha = 0.5
}, completion: nil)
}
}
override func dismissalTransitionDidEnd(_ completed: Bool) {
self.dimmedBackgroundView.removeFromSuperview()
}
@objc private func backgroundTapped() {
self.presentedViewController.dismiss(animated: true, completion: nil)
}
}
演示
OverlayViewController是普通的ViewController。在这里,我使用了丑陋的绿色背景色,以便于识别叠加层。