8

Tôi đang sử dụng bộ điều khiển chế độ xem để quản lý bộ bộ điều khiển chế độ xem con sẽ có thể hiển thị một cách có điều kiện các chế độ xem khác theo cách tùy chỉnh.Sử dụng definePresentationContext với UIModalPresentationStyle.custom

Tôi đã chạy vào một vấn đề mà các definesPresentationContext tài sản được không được sử dụng khi trình bày từ một bộ điều khiển xem sử dụng UIModalPresentationStyle.custom

Ví dụ, tôi có ba điều khiển xem: ROOT, A, và B

ROOT 
|_ A 

A là con của ROOT. Tôi muốn trình bày B theo phương thức từ A trong khi sử dụng tùy chỉnh UIPresentationController, UIViewControllerTransitioningDelegateUIViewControllerAnimatedTransitioning.

Vì vậy, tôi làm như sau trong mã cho điều khiển A (lưu ý điều khiển AdefinesPresentationContext thiết lập để true):

func buttonPressed(_ sender: Any?) { 
    let presentationController = MyCustomPresentation() 

    let controllerToPresent = B() 

    controllerToPresent.modalTransitionStyle = .custom 
    controllerToPresent.transitioningDelegate = presentationController 

    present(controllerToPresent, animated: true, completion: nil) 
} 

Tuy nhiên, bên trong điều khiển trình bày của tôi (mà cũng là UIViewControllerAnimatedTransitioning của tôi) tôi gặp phải như sau vấn đề:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { 
    let fromVC = transitionContext.viewController(forKey: .from) 
    let toVC = transitionContext.viewController(forKey: .to) 

    if let fromVC = fromVC as? A, 
     let toVC = toVC as? B { 
     //Do the presentation from A to B 
    } 
} 

trong chức năng này, nơi mà tôi mong đợi fromVC là loại A, nó thực sự là ROOT. Mặc dù thực tế là A chỉ định definesPresentationContext.

Vì vậy, tôi hình dung điều này là vì tôi đang sử dụng UIModalPresentationStyle.custom. Vì vậy, tôi thay đổi nó để UIModalPresentationStyle.overCurrentContext

Điều này làm cho iOS để đọc một cách chính xác các definesPresentationContext tài sản từ A, và chức năng animateTransition của tôi bây giờ được gọi với đúng từ quan điểm điều khiển, nhưng:

Bởi vì phong cách trình bày phương thức của tôi là không còn .custom, các phương pháp sau đây trong đang chuyển đổi của tôi đại biểu không còn được gọi

func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? 

Vì vậy, điều khiển trình bày của tôi trở nên không sử dụng.

Tôi muốn một kiểu chuyển đổi phương thức .custom tôn trọng definesPresentationContext. Điều này có thể không? Tui bỏ lỡ điều gì vậy?

Về cơ bản, tôi muốn có một bản trình bày phương thức tùy chỉnh trong ngữ cảnh hiện tại.

+0

Bạn đã thử đặt đại biểu chuyển đổi cũng trong 'A'? Trước dòng này: 'hiện tại (controllerĐể trình bày, hoạt ảnh: true, completion: nil)'. thử điều này: 'self.transitioningDelegate = presentationController' Tôi đề nghị này khi sử dụng: 'UIModalPresentationStyle.overCurrentContext' – zero

Trả lời

0

Trong UIPresentationController lớp con của bạn, ghi đè shouldPresentInFullscreen như sau:

override var shouldPresentInFullscreen: Bool { 
    get { 
     return false 
    } 
} 

Theo UIPresentationController tiêu đề:

// By default each new presentation is full screen. 
// This behavior can be overriden with the following method to force a current context presentation. 
// (Default: YES) 
@property(nonatomic, readonly) BOOL shouldPresentInFullscreen; 

này cùng với definesPresentationContext nên làm các trick.

+1

Cảm ơn bạn đã trả lời của bạn. Tôi đã kiểm tra lớp con UIPresentationController của mình và thấy nó đã trả về 'false' cho' shouldPresentInFullscreen' - nhưng nó vẫn không cho phép trình điều khiển xem tiếp tục xuống cây xác định bối cảnh trình bày và thay vào đó lấy gốc vc. – simeon