我的申请不是为了风景而做的。但是当我打开我的 YouTube 频道时UIWeb视图和一个用户发布视频,它出现在肖像中。我想让它在用户旋转 iPhone 时以横向模式显示。
How to 启用横向模式如本例所示?
我知道有一些“肮脏的黑客”可以做到这一点,但我更喜欢更干净的东西。另外,我不希望 UIWebView 切换为横向,但只是视频可以。
我最终调整了我的视图,使其支持横向模式,使用以下代码:
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = NO;
}
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)