2016-03-03 42 views
0

的視圖控制器在問題被嵌入在UINavigationController和呈現爲.FormSheet像這樣:覆蓋港前賽績尺寸類

class PLViewController:UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationController?.navigationBar.translucent = false 
    } 

    /// Embeds self into a UINavigationController, adds a "done" button to the navVC and uses the passed ViewController to present self embedded in the NavigationController. 
    /// - Parameters: 
    ///  - presentingVC: ViewController which will present the formSheet. 
    ///  - animated: If TRUE, the presentation of the formsheet will be animated. 
    func presentAsFormSheet (presentingVC:UIViewController, animated:Bool, completion:(() -> Void)?) { 
     let navVC = UINavigationController(rootViewController: self) 
     navVC.modalPresentationStyle = .FormSheet 
     let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("dismissFormSheet")) 
     doneButton.tintColor = GlobalVars.cautionColor 
     navigationItem.setLeftBarButtonItem(doneButton, animated: false) 
     presentingVC.presentViewController(navVC, animated: true, completion: completion) 
    } 

    /// Dismisses this ViewController with animation from a modal state. 
    func dismissFormSheet() { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

} 

當VC在「浮動」港前賽績方式(非全屏模式)被呈現它需要基本上表現爲如下所示:

enter image description here

此外,佈局需要,如果應用程序是在任一1/3分割屏幕進行進一步操作(但不是三分之二的)或在iPhone上 一般來說。 iPhone部分很容易弄清楚,基本上檢查設備類型並在代碼中做出相應的響應。

是否有知道,在iPad上,a)在分屏模式和b)使用1/3,1/2或2/3?

回答

0

成功!通過繼承UIViewController,我能夠創建一些方法來以編程方式調整.FormSheet模式,以響應呈現VC的特徵更改。

的UIViewController子類:

class MyViewControllerSubclass: UIViewController { 
private func deviceOrientation() -> UIDeviceOrientation { 
     return UIDevice.currentDevice().orientation 
    } 

    private func getScreenSize() -> (description:String, size:CGRect) { 
     let size = UIScreen.mainScreen().bounds 
     let str = "SCREEN SIZE:\nwidth: \(size.width)\nheight: \(size.height)" 
     return (str, size) 
    } 

    private func getApplicationSize() -> (description:String, size:CGRect) { 
     let size = UIApplication.sharedApplication().windows[0].bounds 
     let str = "\n\nAPPLICATION SIZE:\nwidth: \(size.width)\nheight: \(size.height)" 
     return (str, size) 
    } 

    /// Called when the UIViewController's traitCollection has changed. 
    /// - Description: 
    /// Include any changes that need to be made when the trait collection changes. 
    func respondToSizeChange (layoutStyle:LayoutStyle) { 

    } 


    enum LayoutStyle: String { 
     case iPadFullscreen   = "iPad Full Screen" 
     case iPadHalfScreen   = "iPad 1/2 Screen" 
     case iPadTwoThirdScreen  = "iPad 2/3 Screen" 
     case iPadOneThirdScreen  = "iPad 1/3 Screen" 
     case iPhoneFullScreen  = "iPhone" 
    } 

    /// - Returns: a `LayoutStyle` value containing information about what portion of the screen the application is consuming. 
    internal func determineLayout() -> LayoutStyle { 
     if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
      return .iPhoneFullScreen 
     } 
     let screenSize = getScreenSize().size 
     let appSize = getApplicationSize().size 
     //let orientation = deviceOrientation() 
     let screenWidth = screenSize.width 
     let appWidth = appSize.width 
     if screenSize == appSize { 
      return .iPadFullscreen 
     } 

     if deviceOrientation() == .Portrait && screenSize != appSize { 
      return .iPadOneThirdScreen 
     } 
     // In case the math isn't exact, set a range. 
     let lowRange = screenWidth - 15 
     let highRange = screenWidth + 15 

     if lowRange/2 <= appWidth && appWidth <= highRange/2 { 
      return .iPadHalfScreen 
     } else if appWidth <= highRange/3 { 
      return .iPadOneThirdScreen 
     } else { 
      return .iPadTwoThirdScreen 
     } 

    } 

    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { 
     super.traitCollectionDidChange(previousTraitCollection) 
     respondToSizeChange(determineLayout()) 
    } 
} 

而就的ViewController呈現模態:

class MainViewController:UIViewController { 

    /// The modally-presented formSheet. 
    var presentedFormSheet:MyViewControllerSubclass? 

    /// Modally presents the VC in `.FormSheet` style and sets the local `presentedFormSheet` variable to the VC. 
    func showContentRequirementVC() { 
     let vc = MyViewControllerSubclass(nibName: "myCustomViewController", bundle: nil) 
     vc.modalPresentationStyle = .FormSheet 
     presentedFormSheet = vc 
     presentViewController(vc, animated: true, completion: nil) 
    } 

    // When the trait collection changes, tell the presentedFormSheet about it. 
    override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) { 
     presentedFormSheet?.respondToSizeChange(determineLayout()) 
    } 
} 

結果: 如下面的屏幕截圖中所示,紅色實現在只有當表格消耗應用程序的整個區域時纔會有條形圖。在傳遞的數據中有足夠的信息,您可以單獨迴應完整的2/3,1/2和1/3。模態也將對變化做出響應,即使它已經出現。

下面的屏幕截圖也顯示了這個問題是相關的原因。正如你所看到的,並排桌子很好,直到我們達到1/3或iPhone。此時,它們需要位於標籤欄控制器上或使用其他方法,以便一次只顯示一個表格,並且用戶可以在它們之間切換。這個特定的VC使用UIStackViews構建,因此可以通過編程方式輕鬆地調整行爲以適應UI佈局。

全屏 full-screen

** 2/3屏幕** 2/3 screen

1/2屏幕 1/2 screen

1/3屏幕 1/3 screen

滑在 slide-over