每个视图上都有相同的 WebView

发布于 2024-10-07 18:53:28 字数 332 浏览 1 评论 0原文

基本上我在 SecondViewController 上有一个 WebView ,我希望 WebView 在每个视图上都可见,就像选项卡栏一样,并且在每个视图上完全可控。

请注意 WebView 将位于带有在线幻灯片的网页上,因此我不能简单地在每个视图上重新加载

另外在 SecondViewController 中我有

- (void)webViewDidFinishLoad:(UIWebView *)YouTubePlayer {

Basically i have a WebView on SecondViewController and I wish for the WebView to be visible on every view like a tab bar and fully controllable on each view.

Please note the WebView will be on a webpage with a online slideshow so I cannot simply reload on each view

Also in the SecondViewController I have

- (void)webViewDidFinishLoad:(UIWebView *)YouTubePlayer {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我乃一代侩神 2024-10-14 18:53:28

我建议在添加 tabbarcontroller.view 之后在窗口上添加 webView ,就像:

[window addSubview:tabbarController.view];
[window addSubview:webview];
[window makeKeyAndVisible];

并且最初使其不可见。您应该在应用程序委托中处理所有与 webview 相关的方法。现在,只要您不需要它,您就可以通过从视图控制器调用您在应用程序委托中编写的方法来隐藏它。

希望这有帮助。

I would suggest adding the webView on you window after the you add the tabbarcontroller.view just like:

[window addSubview:tabbarController.view];
[window addSubview:webview];
[window makeKeyAndVisible];

and initially make don't make it visible. You should handle all the webview related methods in the app delegate. Now whenever you don't need it you can hide it by calling the methods your wrote in app delegate from your view controllers.

Hope this helps.

我的影子我的梦 2024-10-14 18:53:28

我只是设置一个单例 UIWebView 并在该视图控制器即将变得可见时将其添加到每个视图控制器视图中。这是一种方法:

//.h

@interface SharedWebView : UIWebView
{
}

+ (SharedWebView*) shared;

@end

//.m

SharedWebView* g_sharedWebView;

@implementation SharedWebView


+ (SharedWebView*) shared 
{
    if ( g_sharedWebView == nil )
    {
        g_sharedWebView = [[SharedWebView alloc] init];

        // ... any other intialization you want to do
    }

    return g_sharedWebView;
}

@end


// in your view controller(s)

@property (readonly) UIWebView* webView

- (UIWebView*) webView
{
    return [SharedWebView shared];
}

- (void) viewWillAppear: (BOOL) animated
{
    [super viewWillAppear: animated];

    [self.view addSubview: self.webView ];
    self.webView.frame = CGRectMake(10, 10, 300, 300);

    // want to re-set the delegate?
    // self.webView.delegate = self;
}

I'd just set up a singleton UIWebView and add it to each view-controller-view when that view controller is about to become visible. Here's one way to do it:

//.h

@interface SharedWebView : UIWebView
{
}

+ (SharedWebView*) shared;

@end

//.m

SharedWebView* g_sharedWebView;

@implementation SharedWebView


+ (SharedWebView*) shared 
{
    if ( g_sharedWebView == nil )
    {
        g_sharedWebView = [[SharedWebView alloc] init];

        // ... any other intialization you want to do
    }

    return g_sharedWebView;
}

@end


// in your view controller(s)

@property (readonly) UIWebView* webView

- (UIWebView*) webView
{
    return [SharedWebView shared];
}

- (void) viewWillAppear: (BOOL) animated
{
    [super viewWillAppear: animated];

    [self.view addSubview: self.webView ];
    self.webView.frame = CGRectMake(10, 10, 300, 300);

    // want to re-set the delegate?
    // self.webView.delegate = self;
}
淡淡離愁欲言轉身 2024-10-14 18:53:28

最简单的方法是让所有视图控制器都知道这个额外的视图(通过单例 PinnedViewController 或其他方式使视图可用)。在每个视图控制器的 -viewWillAppear 期间,只需执行以下操作:

[self addSubview:[[PinnedViewController sharedController] view]];

这会将视图移动到当前活动的视图(使视图成为子视图会自动将您从旧层次结构中删除)。

如果这很麻烦或不可行,还有其他两种选择。首先,您可以子类化 UITabViewController (我假设这就是您在问题中使用的内容),并注入额外的视图(调整内容视图的大小以腾出空间)。这是无证且不受支持的,所以要小心。但如果你不做太多其他花哨的技巧,这并不是非常困难。

另一个棘手的解决方案是创建第二个 UIWindow ,将其浮动在主 UIWindow 上(或调整主 UIWindow 的大小以为其腾出空间) 。这只是半文档化的,也没有得到真正的支持。但是,如果您尝试将额外的视图放在选项卡栏下方,那么这种方法就可以工作。

但如果您的系统足够简单,我建议让您的视图控制器全部手动管理固定视图。通过这种方式,您将节省大量的代码,并且不必依赖任何未记录的内部视图层次结构。

The simplest approach is to just make all of your view controllers aware of this extra view (make the view available through a singleton PinnedViewController or whatever). During each view controller's -viewWillAppear, just do this:

[self addSubview:[[PinnedViewController sharedController] view]];

This will move the view to whoever is currently active (making a view your subview automatically removes you from your old hierarchy).

If that is cumbersome or otherwise unworkable, there are two other options. First, you can subclass UITabViewController (I assume that's what you're using here from your question), and inject your extra view (resizing the content view to make room). This is undocumented and unsupported, so take heed. But it's not incredibly difficult if you don't do too many other fancy tricks.

The other tricky solution is to create a second UIWindow that you float over the main UIWindow (or resize the main UIWindow to make room for it). This is only semi-documented and is also not really supported. But this approach can work if you're trying to put the extra view below the tabbar for instance.

But if your system is simple enough, I recommend just letting your view controllers all manage the pinned view manually. You'll save a lot of code spelunking that way, and you won't have to rely on any undocumented internal view hierarchies.

独孤求败 2024-10-14 18:53:28

听起来您试图将视图放在该视图之上,但不是模态的。我曾经看到过一篇博客文章,描述了如何做这样的事情。我认为它也应该适用于您的情况: iPhone 上的半模式透明对话框

Sounds like you try to put views on top of this view but not modal. There was this blog entry I once saw that described how you would do something like this. I think it should apply also for your case: semi-modal-transparent-dialogs-on-the-iphone

流年已逝 2024-10-14 18:53:28

在 iOS 中,UIViewController 需要管理整个“屏幕”的内容,因此尝试在多个视图控制器之间共享单个视图是不正常的。尝试让 UIViewController 的视图仅管理其窗口的一部分是有问题的,并且会导致意外行为,因为 UIKit 不会向所有具有可见视图的视图控制器发送类似 -viewWillAppear 的消息。相反,您通常会创建一个 UIViewController,其视图包括该 Web 视图以及构成您的选项卡界面的任何其他视图。
或者,您可以拥有许多视图控制器的层次结构,并添加一个 Web 视图作为所有视图控制器的子视图。然后,您可以将 Web 视图委托行为拉出到某个非 UIViewController 控制器类中以管理 Web 视图的行为。

In iOS UIViewControllers are expected to manage an entire "screen" worth of content so it's not normal to try to share a single view across many view controllers. Trying to have UIViewControllers whose views only manage part of their window is problematic and will result in unexpected behavior as UIKit will not send messages like -viewWillAppear to all view controllers with visible views. Instead you would normally create a single UIViewController whose view includes that web view and whatever other views compose your tab like interface.
Alternately you could have a hierarchy of many view controllers and add a single web view as a subview of all of them. You would then pull your web view delegate behavior out into some non-UIViewController controller class to manage the behavior of the web view.

陌生 2024-10-14 18:53:28

您可以让所有视图占据屏幕的一部分,并让 UIWebView 占据其余部分。在其他视图之间切换的逻辑应该保持相同。

例如,在 UIViewControllerviewDidLoad 方法中,您可能有类似的内容:

self.view.frame = CGRectMake(0, 100, 320, 380);

在您的(比如说)AppDelegate 中,您将拥有您正常调用显示主 UIViewController (在您的情况下,它听起来像 UITabBarController?),并且还调用添加 UIWebView 。比如说:

myWebView.view.frame = CGRectMake(0, 0, 320, 100);

视图控制器和 UIWebView 将彼此独立。

我相信,您会在带有 iAd 的应用程序中看到这种模式。我已经用我的一个免费应用程序做了类似的事情。

希望这有帮助!

You can have all your views take up a portion of the screen and have your UIWebView take up the rest. The logic for switching between the other views should remain the same.

For example, in your viewDidLoad method for your UIViewControllers, you could have something like:

self.view.frame = CGRectMake(0, 100, 320, 380);

And in your (say) AppDelegate, you would have your normal call to show the main UIViewController (in your case, it sounds like a UITabBarController?) and also have a call to add the UIWebView. Say, something like:

myWebView.view.frame = CGRectMake(0, 0, 320, 100);

The view controllers and UIWebView would be independent of each other.

You see this pattern, I believe, with apps that have iAds. I've done something just like this with one of my free apps.

Hope this helps!

懒猫 2024-10-14 18:53:28

我通过简单地将其添加到 navigationController 本身获得了相同的效果(如果您没有,则只需添加它)。
在我的一款应用程序中非常适合我。

I got the same effect by simply adding it to the navigationController itself(if you don't have one then just add it).
works great for me in one of my apps.

音盲 2024-10-14 18:53:28

您能否不在应用程序上使用两个 Web 视图,而只需使用更动态的内容更改最上面的 Web 视图?

Could you not use two webviews on your application and simply change the uppermost webview with your more dynamic content?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文