iOS和Objective-C:当我点击它上面的链接时,为什么我的UIWebView会全屏显示?

时间:2013-12-07 00:46:08

标签: iphone objective-c uiwebview uiwebviewdelegate

起初,对不起我的英语不好,我只是在学习它。我在Objective-C相对较新,我正在使用FBConnect包将我的应用程序链接到Facebook。所有它都正常工作,但是当我点击UIWebView(例如Facebook登录按钮)的链接时,我的UIWebView会全屏显示并隐藏我的关闭按钮(UIWebView右上角的一个小'x'按钮,在这里调用“_closeButton”)。

以下是代码:

“init”方法:

- (id)init {
  if ((self = [super initWithFrame:CGRectZero])) {
    _delegate = nil;
    _loadingURL = nil;
    _showingKeyboard = NO;

    self.backgroundColor = [UIColor clearColor];
    self.autoresizesSubviews = NO;
    //self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.contentMode = UIViewContentModeRedraw;

    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(kPadding, kPadding, 480, 480)];
    _webView.delegate = self;
    //_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:_webView];



    //UIImage* closeImage = [UIImage imageNamed:@"FBDialog.bundle/images/close.png"];
    UIImage* closeImage = [UIImage imageNamed:@"close"];

    UIColor* color = [UIColor colorWithRed:255.0/255 green:/*184.0*/0.0/255 blue:/*216.0*/0.0/255 alpha:1];
    _closeButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
    [_closeButton setImage:closeImage forState:UIControlStateNormal];
    [_closeButton setTitleColor:color forState:UIControlStateNormal];
    [_closeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [_closeButton addTarget:self action:@selector(cancel)
           forControlEvents:UIControlEventTouchUpInside];

    // To be compatible with OS 2.x
#if __IPHONE_OS_VERSION_MAX_ALLOWED <= __IPHONE_2_2
    _closeButton.font = [UIFont boldSystemFontOfSize:12];
#else
    _closeButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
#endif

    _closeButton.showsTouchWhenHighlighted = YES;
    _closeButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin
    | UIViewAutoresizingFlexibleBottomMargin;
    [self addSubview:_closeButton];
    NSLog(@"close");

    _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
                UIActivityIndicatorViewStyleWhiteLarge];
    _spinner.autoresizingMask =
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [self addSubview:_spinner];
    _modalBackgroundView = [[UIView alloc] init];
}
return self;
}

WebView的代表(我怀疑这里是我需要做的伎俩)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType {
NSURL* url = request.URL;

if ([url.scheme isEqualToString:@"fbconnect"]) {
    if ([[url.resourceSpecifier substringToIndex:8] isEqualToString:@"//cancel"]) {
        NSString * errorCode = [self getStringFromUrl:[url absoluteString] needle:@"error_code="];
        NSString * errorStr = [self getStringFromUrl:[url absoluteString] needle:@"error_msg="];
        if (errorCode) {
            NSDictionary * errorData = [NSDictionary dictionaryWithObject:errorStr forKey:@"error_msg"];
            NSError * error = [NSError errorWithDomain:@"facebookErrDomain"
                                                  code:[errorCode intValue]
                                              userInfo:errorData];
            [self dismissWithError:error animated:YES];
        } else {
            [self dialogDidCancel:url];
        }
    } else {
        [self dialogDidSucceed:url];
    }
    return NO;
} else if ([_loadingURL isEqual:url]) {
    return YES;
} else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    if ([_delegate respondsToSelector:@selector(dialog:shouldOpenURLInExternalBrowser:)]) {
        if (![_delegate dialog:self shouldOpenURLInExternalBrowser:url]) {
            return NO;
        }
    }

    [[UIApplication sharedApplication] openURL:request.URL];
    return NO;
} else {
    return YES;
}
}

show方法(当我需要显示Web View时调用)

- (void)show {
NSLog(@"Cuantas veces me ves?");
[self load];
[self sizeToFitOrientation:NO];

CGFloat innerWidth = self.frame.size.width - (kBorderWidth+1)*2;
[_closeButton sizeToFit];

_closeButton.frame = CGRectMake(
                                2,
                                2,
                                29,
                                29);

_webView.frame = CGRectMake(
                            kBorderWidth+1,
                            kBorderWidth+1,
                            innerWidth,
                            self.frame.size.height - (1 + kBorderWidth*2));

[_spinner sizeToFit];
[_spinner startAnimating];
_spinner.center = _webView.center;
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}

_modalBackgroundView.frame = window.frame;
[_modalBackgroundView addSubview:self];
[window addSubview:_modalBackgroundView];

[window addSubview:self];

[self dialogWillAppear];

self.transform = CGAffineTransformScale([self transformForOrientation], 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kTransitionDuration/1.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)];
self.transform = CGAffineTransformScale([self transformForOrientation], 1.1, 1.1);
[UIView commitAnimations];

[self addObservers];
}

通过Google搜索问题,我发现这可能是UIWebView的默认行为。如何更改它以使我的所有WebViews与第一个相同?

提前致谢!

==编辑==

仍然不知道为什么我的UIWebView会像这样,或者如何解决它,但由于代码是FBConnect Bundle的一部分,我想我不应该编辑太多。我刚刚以编程方式在顶部创建了一个新的关闭按钮,这就是全部。但如果有人知道如何解决问题,我会打开这个问题。感谢Leo Natan的帮助。

0 个答案:

没有答案