use*_*714 17 resize objective-c uiwebview orientation
我在viewcontroller的视图上添加了一个webview,一个titleLabel和一个coverflowView作为其子视图,我希望它在方向改变时改变大小.我在这个方法中更改了webview的框架:
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration
Run Code Online (Sandbox Code Playgroud)
如果我以纵向方向启动应用程序,当将iPad从orientationLandscape旋转到orientationPortrait或从orientationPortrait旋转到orientationLandscape时,其内容会调整大小,但是当我以横向方向启动应用程序时,它的内容不会调整大小......但是NSLog显示帧已更改.至于titleLabel和coverflowView,它们可以正确调整大小.我怀疑是不是因为css?我根据webview的高度和宽度使用css控制内容的样式.有人能帮我找到原因吗?代码如下:
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
double i = 0;
NSInteger width=self.view.frame.size.width;
NSInteger height=self.view.frame.size.height;
NSLog(@"view :%@",[self.view description]);
switch (toInterfaceOrientation){
case UIInterfaceOrientationPortrait:
{
NSLog(@"rotate to Portrait");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
self.docView.frame=CGRectMake(0, 50, width+20, height-70);
self.toolbar.frame=CGRectMake(0, 0,height , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag){
view.frame=CGRectMake(width-60, 6, 50, 36);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(width-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , width+20, height/2-50)];
[titleLabel setFrame:CGRectMake(width/2-40,height/2-100, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 768, 1004);
}else{
self.viewer.frame=CGRectMake(0, 0, 320, 480);
}
}
i=0;
}break;
case UIInterfaceOrientationPortraitUpsideDown:
{
NSLog(@"rotate to PortraitUpsideDown");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
self.docView.frame=CGRectMake(0, 50, width+20, height-70);
self.toolbar.frame=CGRectMake(0, 0,height , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(width-60, 6, 50, 36);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(width-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , width+20, height/2-50)];
[titleLabel setFrame:CGRectMake(width/2-40,height/2-100, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 768, 1004);
}else{
self.viewer.frame=CGRectMake(0, 0, 320, 480);
}
}
i=180;
} break;
case UIInterfaceOrientationLandscapeLeft:{
NSLog(@"rotate to LandscapeLeft");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// self.coverflow.frame=CGRectMake(0, 0, height+20, width-20);
self.docView.frame=CGRectMake(0, 50, height+20, width-70);
self.toolbar.frame=CGRectMake(0, 0,height+20 , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(height-60, 6, 50, 36);
NSLog(@"button %@",[view description]);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(height-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , height+20, width/2-50)];
[titleLabel setFrame:CGRectMake(height/2-40,width/2-80, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 1024, 748);
}else{
self.viewer.frame=CGRectMake(0, 0, 480, 320);
}
}
i = 90;
}break;
case UIInterfaceOrientationLandscapeRight:{
NSLog(@"rotate to LandscapeRight");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// self.coverflow.frame=CGRectMake(0, 0, height+20, width-20);
self.docView.frame=CGRectMake(0, 50, height+20, width-70);
self.toolbar.frame=CGRectMake(0, 0,height+20 , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(height-60, 6, 50, 36);
}
else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(height-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , height+20, width/2-50)];
[titleLabel setFrame:CGRectMake(height/2-40,width/2-80, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 1024, 748);
}else{
self.viewer.frame=CGRectMake(0, 0, 480, 320);
}
}
i = -90;
}break;
}
//[webViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// [self.view setNeedsDisplay];
// NSLog(@"coverflowView :%@",[self.coverflow description]);
NSLog(@"webview :%@",[viewer description]);
[viewer stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.__defineGetter__('orientation',function(){return %f;});",i]];
[viewer stringByEvaluatingJavaScriptFromString:@"var e = document.createEvent('Events'); e.initEvent('orientationchange', true, false); document.dispatchEvent(e); "];
}
- (void)viewDidLoad {
self.view.clipsToBounds=YES;
self.view.autoresizesSubviews=YES;
// self.view.autoresizingMask=UIViewAutoresizingNone;
viewer=[[UIWebView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:viewer];
viewer.delegate=self;
viewer.scalesPageToFit=NO;
viewer.autoresizesSubviews=NO;
viewer.autoresizingMask=UIViewAutoresizingNone;
viewer.dataDetectorTypes=0;
// viewer.autoresizingMask=UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
NSLog(@"webView :%@",[viewer description]);
// [viewer setFrame:CGRectMake(0, self.view.bounds.size.height/2 , self.view.bounds.size.width, self.view.bounds.size.height/2)];
// [viewer setBounds:CGRectMake(0, self.view.bounds.size.height/2 , self.view.bounds.size.width, self.view.bounds.size.height/2)];
UIScrollView *scroller=[viewer.subviews objectAtIndex: 0];
if (scroller) {
scroller.alwaysBounceVertical=NO;
scroller.bounces=NO;
scroller.scrollEnabled=NO;
}
[self viewHomePage];
//[self createCoverFlowView];
[self createPopView];
//[self setHomeButtonPosition];
//[self setSettingButtonPosition];
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30000
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:longPress];
longPress.minimumPressDuration=2.0;
longPress.delegate = self;
longPress.cancelsTouchesInView = NO;
longPress.allowableMovement=20;
[longPress release];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];
singleTap.delegate = self;
singleTap.cancelsTouchesInView = NO;
[singleTap release];
#endif
//[viewer setOpaque:YES]; //??
[super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)
chr*_*ton 51
我花了大约两天试图追踪这个问题,因为我遇到了完全相同的问题.我发现我的视图如果以肖像模式开始,将正确调整为横向.但是,如果我在横向模式下启动视图,它将在纵向中保持相同的大小.
如果您的视图是设备的整个宽度,则只需添加以下元标记即可.
<meta name="viewport" content="width=device-width" />
Run Code Online (Sandbox Code Playgroud)
但是,就我而言,我们的UIWebView是一个非标准的大小.将视口设置为device-width更糟糕!通过使用javascript动态设置视口,我能够正常运行.这应该在UIWebViews父视图的layoutSubviews期间完成.您可以在willRotateToInterfaceOrientation中执行此操作,但我发现如果您使用willRotate,则可能无法正确设置某些大小和接口.首先尝试一下,看看动画是如何工作的.
代码只是将视口大小设置为当前帧大小.您正在查看的HTML必须已有meta viewport标记(如上所述).如果您无法编辑HTML,还可以通过javascript添加其他方法.最简单的方法是将元标记添加到<head>标记中.然后在正确设置框架后使用以下代码:
// assuming your self.viewer is a UIWebView
[self.viewer stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:
@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
(int)self.viewer.frame.size.width]];
Run Code Online (Sandbox Code Playgroud)
如果您不希望用户缩放,或者您想设置最大比例,则可以添加其他视口项.这最终使我们的UIWebView的视图行为正常.
小智 1
我有类似的问题,但仍然找不到真正的解决方案。我在 UIWebView 上调用重新加载来解决该问题,方法是让比例页面适合。
[webView reload];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32215 次 |
| 最近记录: |