我希望这个问题不是一个愚蠢的问题,但是如何将 UIPopoverController View 定位在 UIWebView 上,以便弹出 View 箭头指向被单击以显示它的 UIWebView 链接?
我正在使用委托(delegate)方法;
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] absoluteString] hasPrefix:@"myscheme:"] ) {
//UIPopoverController stuff here
return NO;
}
}
捕获和路由点击,但我不确定如何获取链接坐标来定位弹出 View 。
非常感谢任何帮助或指向相关信息的指针。
最佳答案
我有一个可行的解决方案,但我认为有更好的方法来做到这一点。
我创建了一个继承自 UIWebView 的 TouchableWebView,并将触摸位置保存在方法 hitTest 中。之后,您需要为您的 webview 设置一个委托(delegate)并实现方法 -webView:shouldStartLoadWithRequest:navigationType:
TouchableWebView.h:
@interface TouchableWebView : UIWebView {
CGPoint lastTouchPosition;
}
@property (nonatomic, assign) CGPoint lastTouchPosition;
TouchableWebView.m:
// I need to retrieve the touch position in order to position the popover
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
self.lastTouchPosition = point;
return [super hitTest:point withEvent:event];
}
在 RootViewController.m 中:
[self.touchableWebView setDelegate:self];
// WebView delegate, when hyperlink clicked
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
CGPoint touchPosition = [self.view convertPoint:self.touchableWebView.lastTouchPosition fromView:self.touchableWebView];
[self displayPopOver:request atPosition:touchPosition isOnCelebrityFrame:FALSE];
return FALSE;
}
return TRUE;
}
这不好,因为
UIWebView
的文档说你不应该继承它。我想有一个更好的方法,但这确实有效。你找到另一个解决方案了吗?
关于cocoa-touch - iPad UIWebView 将 UIPopoverController View 定位在 href 链接坐标处,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3118401/