Modal View Controller Won't Start in Landscape Mode

后端 未结 7 1430
死守一世寂寞
死守一世寂寞 2020-12-10 17:10

I have a navigation based app that has a detail view (UIWebView) with action buttons across the bottom in a UIToolbar. I want to add \'notes\' when the \'notes\' button is

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 17:52

    Wow, I lost days over that issue ... but I found a solution!

    I had the same problem you had: the method "presentModalViewController:animated:" only worked in portrait mode.

    After a lot of trial and error, I found out that the reason was that I had several view controllers active at the same time. I implemented a navigation system which switched between different view controllers, with one parent handling the children. (I could not use UINavigationController, because I needed a different look.)

    So, my root view controller had a root view object, and several child view controllers. When a child view controller was activated, its view object was added as subview to the view of the root view controller.

    The "presentModalViewController" method didn't like that. However, as soon as I set the "parentViewController" property of the child view controllers, it worked!

    The problem is only that "parentViewController" is a read-only property. You have to extend the UIViewController class so you can access it.

    @interface UIViewController (HelperExtension)
    
    @property (nonatomic, assign) UIViewController *parent;
    
    @end
    
    @implementation UIViewController (HelperExtension)
    
    - (UIViewController *)parent
    {
        return self.parentViewController;
    }
    
    - (void)setParent:(UIViewController *)parent
    {
        [self setValue:parent forKey:@"_parentViewController"];
    }
    
    @end
    

    So, whenever you add the view of a child view controller to your parent view controller, call the "setParent:" method after doing it. Then it will work!

提交回复
热议问题