Change the width of Master in UISplitViewController

前端 未结 16 1711
自闭症患者
自闭症患者 2020-11-28 05:55

The iPad programming guide says that the splitView\'s left pane is fixed to 320 points. But 320 pixels for my master view controller is too much. I would like to reduce it a

16条回答
  •  情深已故
    2020-11-28 06:40

    If you subclass UISplitViewController, you can implement -viewDidLayoutSubviews and adjust the width there. This is clean, no hacks or private APIs, and works even with rotation.

    - (void)viewDidLayoutSubviews
    {
        const CGFloat kMasterViewWidth = 240.0;
    
        UIViewController *masterViewController = [self.viewControllers objectAtIndex:0];
        UIViewController *detailViewController = [self.viewControllers objectAtIndex:1];
    
        if (detailViewController.view.frame.origin.x > 0.0) {
            // Adjust the width of the master view
            CGRect masterViewFrame = masterViewController.view.frame;
            CGFloat deltaX = masterViewFrame.size.width - kMasterViewWidth;
            masterViewFrame.size.width -= deltaX;
            masterViewController.view.frame = masterViewFrame;
    
            // Adjust the width of the detail view
            CGRect detailViewFrame = detailViewController.view.frame;
            detailViewFrame.origin.x -= deltaX;
            detailViewFrame.size.width += deltaX;
            detailViewController.view.frame = detailViewFrame;
    
            [masterViewController.view setNeedsLayout];
            [detailViewController.view setNeedsLayout];
        }
    }
    

提交回复
热议问题