2015-05-21 17 views
24

Tôi muốn luôn hiển thị ViewController trong cửa sổ bật lên trên tất cả các thiết bị và mọi hướng. Tôi đã cố gắng thực hiện điều này bằng cách áp dụng UIPopoverPresentationControllerDelegate và đặt sourceViewsourceRect.UIModalPresentationPopover for iPhone 6 Plus in landscape không hiển thị cửa sổ bật lên

Tính năng này hoạt động rất tốt cho tất cả các thiết bị và định hướng, ngoại trừ iPhone 6 Plus ở chế độ ngang. Trong trường hợp đó, trình điều khiển chế độ xem sẽ trượt lên từ cuối màn hình trong một biểu mẫu. Làm thế nào tôi có thể ngăn chặn điều đó để nó sẽ luôn xuất hiện trong một cửa sổ bật lên?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
let popoverPresentationController = segue.destinationViewController.popoverPresentationController 
popoverPresentationController?.delegate = self 
popoverPresentationController?.sourceView = self.titleLabel!.superview 
popoverPresentationController?.sourceRect = self.titleLabel!.frame } 

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { 
return UIModalPresentationStyle.None } 

Tất cả các thiết bị đang bị iOS 8.2 hoặc cao hơn

Trả lời

57

Thực hiện phương pháp mới adaptivePresentationStyleForPresentationController:traitCollection: của UIAdaptivePresentationControllerDelegate:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { 
    // This method is called in iOS 8.3 or later regardless of trait collection, in which case use the original presentation style (UIModalPresentationNone signals no adaptation) 
    return UIModalPresentationNone; 
} 

UIModalPresentationNone yêu cầu trình điều khiển trình bày sử dụng giá trị gốc phong cách ntation mà trong trường hợp của bạn sẽ hiển thị một cửa sổ bật lên.

+1

Cuộc gọi tốt! Tôi quên rằng phương thức ủy nhiệm đã thay đổi cho 8.3. –

+1

@PetahChristian Cảm ơn! Vâng là một thay đổi khá yên tĩnh và dường như không được ghi chép khác ngoài các khác biệt về API. – Joshua

+0

@Joshua YES! Tôi đã thực hiện - (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController *) controller { trả về UIModalPresentationNone; } nhưng API mới giải quyết được vấn đề của tôi, cảm ơn bạn! – Loegic

1

Apple thiết kế trình bày iPhone 6 Plus để hành xử theo cách đó, dựa trên lớp kích thước của nó.

Để ngăn chặn bản trình bày phương thức trên iPhone 6 Plus, bạn sẽ phải ghi đè bộ sưu tập đặc điểm (kích thước ngang).

Bạn sẽ có thể thiết lập các overrideTraitCollection tài sản để điều khiển trình bày: (!. Xin lỗi vì sự C Mục tiêu tôi đã không học được Swift chưa)

presentedVC.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact]; 

+0

Tôi xin lỗi nhưng nó không làm việc cho tôi, tôi không thể ghi đè lên traitCollection , "có nghĩa là" cửa sổ bật lên vẫn được hiển thị dưới dạng trang tính trên iPhone 6 cộng với cảnh quan chỉ – Loegic

+0

Trong khi hấp dẫn, tôi không có thời gian sáng nay để điều tra điều này. Tôi có thể cung cấp một tiền thưởng cho câu hỏi để thu hút một số sự chú ý đến nó. :) –

+0

Tôi đã thấy nó, cảm ơn bạn :) Ghi đè traitCollection dường như là một ý tưởng tuyệt vời, nhưng dường như nó bị bỏ qua. – Loegic

2

Trong Swift 3, nếu bạn thực hiện bản gốc adaptivePresentationStyle phương pháp, chỉ cần thêm đoạn mã này hoạt động:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle { 
    return adaptivePresentationStyle(for: controller) 
} 
Các vấn đề liên quan