【问题标题】:why is the text cuts when changing the orientation为什么更改方向时会剪切文本
【发布时间】:2012-01-21 06:11:15
【问题描述】:

我正在使用 Web 视图来显示 HTML 内容并使用一些方向方法,但是当我第一次将方向表单横向更改为纵向时,文本从视图中消失。当我从纵向滚动到另一个它向我展示了完美的页面。可能是因为从横向到纵向的宽度。

我使用的代码和方法如下:

设置页面大小

- (void)setPageSize:(NSString *)orientation {
NSLog(@"• Set size for orientation: %@", orientation);

pageWidth = screenBounds.size.width;
pageHeight = screenBounds.size.height;
if ([orientation isEqualToString:@"landscape"]) {
    pageWidth = screenBounds.size.height;
    pageHeight = screenBounds.size.width;
}

}

方向

- (NSString *)getCurrentInterfaceOrientation {
if ([availableOrientation isEqualToString:@"portrait"] || [availableOrientation isEqualToString:@"landscape"])
{
    return availableOrientation;
} 
else {
    // WARNING!!! Seems like checking [[UIDevice currentDevice] orientation] against "UIInterfaceOrientationPortrait" is broken (return FALSE with the device in portrait orientation)
    // Safe solution: always check if the device is in landscape orientation, if FALSE then it's in portrait.
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        return @"landscape";
    } else {
        return @"portrait";
    }
}

}

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
if ([availableOrientation isEqualToString:@"portrait"]) {
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
} else if ([availableOrientation isEqualToString:@"landscape"]) {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
} else {
    return YES;
}   

}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
// Notify the index view
[indexViewController willRotate];

// Since the UIWebView doesn't handle orientationchange events correctly we have to do handle them ourselves 
// 1. Set the correct value for window.orientation property
NSString *jsOrientationGetter;
switch (toInterfaceOrientation) {
    case UIDeviceOrientationPortrait:
        jsOrientationGetter = @"window.__defineGetter__('orientation', function() { return 0; });";
        break;
    case UIDeviceOrientationLandscapeLeft:
        jsOrientationGetter = @"window.__defineGetter__('orientation', function() { return 90; });";
        break;
    case UIDeviceOrientationLandscapeRight:
        jsOrientationGetter = @"window.__defineGetter__('orientation', function() { return -90; });";
        break;
    case UIDeviceOrientationPortraitUpsideDown:
        jsOrientationGetter = @"window.__defineGetter__('orientation', function() { return 180; });";
        break;
    default:
        break;
}

// 2. Create and dispatch a orientationchange event    
NSString *jsOrientationChange = @"if (typeof bakerOrientationChangeEvent === 'undefined') {\
                                      var bakerOrientationChangeEvent = document.createEvent('Events');\
                                          bakerOrientationChangeEvent.initEvent('orientationchange', true, false);\
                                  }; window.dispatchEvent(bakerOrientationChangeEvent)";

// 3. Merge the scripts and load them on the current UIWebView
NSString *jsCommand = [jsOrientationGetter stringByAppendingString:jsOrientationChange];
[currPage stringByEvaluatingJavaScriptFromString:jsCommand];

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[indexViewController rotateFromOrientation:fromInterfaceOrientation toOrientation:self.interfaceOrientation];

[self setPageSize:[self getCurrentInterfaceOrientation]];
[self getPageHeight];    
[self resetScrollView];

请帮帮我。我哪里出错了。

【问题讨论】:

  • 你是否正确设置了 webview 的 autoresize 属性?
  • 是的,我已经将 webview 的自动调整大小设置为灵活的高度和宽度

标签: iphone uiwebview xcode4.2 uiinterfaceorientation


【解决方案1】:

我发现问题不在于代码我已经更改了 html 页面的页面大小,它现在可以正常工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2020-10-04
    • 2012-09-30
    • 2015-10-23
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多