我在視圖控制器視圖中添加了一個webview,一個titleLabel和一個coverflowView作爲其子視圖,我希望它在方向更改時更改大小。我有改變的WebView框架在此方法:UIWebView在方向更改時無法正確調整大小?
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration
其內容並調整大小時,將iPad旋轉從orientationLandscape到orientationPortrait或orientationPortrait如果我開始與縱向應用程序orientationLandscape,但它是如此奇怪的是,它的內容當我以橫向方向啓動應用程序時,請不要調整大小...但NSLog顯示框架已更改。至於titleLabel和coverflowView,它們正確調整大小。我懷疑是否是因爲css?我已經使用css根據webview的高度和寬度來控制內容的風格。任何人都可以幫助我找到原因嗎?代碼如下:
- (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];
}
我有這個完全相同的問題。如果應用程序以縱向模式啓動。 UIWebView正確調整大小,我的CSS顯示它已更改。如果我以橫向模式啓動它,它不會再改變。 – christophercotton 2011-05-21 20:23:48