Disabling auto rotation for a UIView

前端 未结 5 1838
谎友^
谎友^ 2021-02-05 07:10

My application is composed of a toolbar and an AVCaptureVideoPreviewLayer in a background UIView. I\'d like to see that toolbar rotate regarding the device orientation, so in my

5条回答
  •  时光取名叫无心
    2021-02-05 07:43

    I finally found a sane answer for this. No more messing around with "anti-rotation animations" and such!

    The key is to use a UIViewController for your preview layer (the non-rotating view) and another for the actual interface. Configure the rotation options for both using shouldAutorotateToInterfaceOrientation: (the code is in the answers above):

    • The preview view controller should be set to not rotate
    • The interface view controller should be set to rotate to the orientations you need

    The important bit is to keep your views separate by adding them directly to the window. I do this in the AppDelegate:

    [self.window addSubview:previewViewController.view];
    [self.window addSubview:interfaceViewController.view];
    self.window.rootViewController = interfaceViewController;
    [self.window makeKeyAndVisible];
    

    That puts your video preview in the background, and then adds the interface view over it. The interface is free to autorotate without affecting the preview at all.

    One important thing to note: the background of your interface view should be transparent, otherwise it blocks the preview.

提交回复
热议问题