Programmatically show / hide master view in UISplitViewController

孤街醉人 提交于 2019-12-08 06:46:21

问题


I have an app that uses a UISplitViewController. When in Landscape orientation, we sometimes want to show the master view all the time and sometimes want it to auto hide as it does in portrait orientation. Currently this setting can be tweaked in app.

This all works well, except for one thing. When I change the setting, I'd like the auto-hide setting to take effect immediately, not just the next time I rotate the device (i.e. when - splitViewController:shouldHideViewController:inOrientation: is called).

Is there some way to (programmatically) force the UISplitViewController to pop out / hide the master view so that the SVC will query the splitViewController:shouldHideViewController:inOrientation: method again?

Any help would be greatly appreciated.


回答1:


There's no straight forward way.

A functional, yet a little hacky solution would be to set a delegate and record the barButtonItem passed to the delegate when showing/hiding the master. You can use it to just trigger the action on the button. But, as I said, this is not really a nice way of doing it (and may break in the future):

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc
{
    _buttonItem = barButtonItem;
    // ...
}

- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)button
{
    _buttonItem = nil;
    // ...
}

- (void)toggleMasterVisible
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [_buttonItem.target performSelector:_buttonItem.action];
#pragma clang diagnostic pop
}



回答2:


For an existing button, you can add this target to achieve what you want:

[button addTarget: theSplitViewController action: @selector(toggleMasterVisible:) forControlEvents:UIControlEventTouchUpInside];

I must assume that means you can just call

[theSplitViewController toggleMasterVisible: nil];

This is entirely undocumented, but it has the same behavior as the barButtonItem you get from the willHideViewController function.



来源:https://stackoverflow.com/questions/15212370/programmatically-show-hide-master-view-in-uisplitviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!