Different Orientation for Different Views?

2019-09-14 06:18发布

问题:

I'm making an iOS application that has an interface in the portrait view.

However, when displaying web content I want the view to display in a landscape format, because I want the website test to display larger initially without the user needing to zoom.

Can I tell the program so present this view only in landscape?

Thank you.

回答1:

looks like you want to force the orientation to landscape mode only..
heres my solution in MyViewController.h
add this code on top of MyViewController's @interface

 //force orientation on device
    @interface UIDevice (PrivateOrientation)
    - (void) setOrientation:(UIInterfaceOrientation)orientation;
    @end

then in the implementation file (MyViewController.m) add this code inside viewWillAppear:

//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];

this will force the device orientation to landscape mode left (or right depending what you want)
if you want to go back to portrait mode after leaving the viewcontroller add this code inside viewWillDisappear:

//change orientation of device
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

finally implement shouldAutorotateToInterfaceOrientation: to force the view into landscape mode left or right (or both)

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

hope this helps :3