2012-01-21 37 views
0

我正在使用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]; 

請幫助me.where做我做錯。

+0

您是否正確設置了webview的autoresize屬性? – iProgrammer

+0

是的,我已經設置了webview的autoresize靈活的高度和寬度 – Purva

回答

0

我發現問題不在於我已經改變了html頁面的頁面大小的代碼,它現在正常工作。

相關問題