如何在 UIWebView 中定位 UIToolbar

发布于 2025-01-07 02:43:59 字数 1794 浏览 2 评论 0原文

我有一个示例项目,其中创建了一个名为 WebView 的自定义 UIWebView。它们被添加到 UIViewController 中的视图中。两个 WebViewviewDidLoad 中初始化并添加到数组列表中。第一个添加为 self.view 的子视图。 Storyboard 包含一个 UIBarButton,当点击它时,第二个 WebView 将作为子视图添加到 self.view 中。

- (void)viewDidLoad
{
    [super viewDidLoad];

    WebView *webView1 = [[WebView alloc] initWithFrame:self.view.bounds];
    WebView *webView2 = [[WebView alloc] initWithFrame:self.view.bounds];
    self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];

    UIWebView *webView = [self.webViews objectAtIndex:0];
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.google.com/"]]];
}

下面是 WebView 的实现,其中工具栏添加到视图的底部(-50px):

@implementation WebView

- (void) initToolbar {
    UIToolbar *toolbar = [UIToolbar new];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [toolbar sizeToFit];
    toolbar.frame = CGRectMake(0, self.frame.size.height-80, 320, 30);

    [self addSubview:toolbar];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self initToolbar];
    }
    return self;
}

@end

问题是,工具栏在两个视图中的位置不同(它们可以与第一个一样定位)。为什么他们的定位不一样?

您可以在此处下载小示例项目: http://uploads.demaweb.dk/UIWebView.zip

我的笔记: 如前所述,两个 WebView 当前已在 viewDidLoad 中初始化。如果我等待并首先初始化,然后当我需要它们时,它似乎会按预期工作。

I've a sample project, where I have created a custom UIWebView named WebView. They are added to the view in a UIViewController. Two WebView's are initialized in the viewDidLoad and added to an array list. The first one is added as subview på self.view. The Storyboard contains a UIBarButton, and when this is tapped, the second WebView is added as subview to the self.view.

- (void)viewDidLoad
{
    [super viewDidLoad];

    WebView *webView1 = [[WebView alloc] initWithFrame:self.view.bounds];
    WebView *webView2 = [[WebView alloc] initWithFrame:self.view.bounds];
    self.webViews = [NSArray arrayWithObjects: webView1, webView2, nil];

    UIWebView *webView = [self.webViews objectAtIndex:0];
    [self.view addSubview:webView];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.google.com/"]]];
}

Here is the implementation of the WebView where a toolbar is added to the bottom (-50px) of the view:

@implementation WebView

- (void) initToolbar {
    UIToolbar *toolbar = [UIToolbar new];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [toolbar sizeToFit];
    toolbar.frame = CGRectMake(0, self.frame.size.height-80, 320, 30);

    [self addSubview:toolbar];
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self initToolbar];
    }
    return self;
}

@end

The problem is, that the toolbar does not have the same position in the two views (they could be positioned like the first one). Why are they not positioned equally?

You can download the small sample project here:
http://uploads.demaweb.dk/UIWebView.zip

My notes: As mentioned, the two WebView's are currently initialized in the viewDidLoad. If I instead wait and first initialize then when I need them, it seems to work as expected.

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

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

发布评论

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

评论(1

爱的十字路口 2025-01-14 02:43:59

这里的问题是,当您通过单击按钮初始化时,viewDidLoad 上的 self.frame.size.height 是不一样的。如果你直接在viewDidLoad中添加UIWebView,你的UINavigationControllerUINavigationBar还没有加载,框架self.view 被“认为”更大(不是真的,因为它更大)。

这就解释了 40.0 (?) 分的差异。

解决此问题行为的一个可能的解决方案是在 viewWillAppear:animated 方法中初始化并添加(至少是第一个)UIWebView 实例。 (UIViewController 已经实现了这个方法)

The thing here is that self.frame.size.height on viewDidLoad is not the same when you initialize it via the button click. If you add the UIWebView directly in viewDidLoad, the UINavigationBar of your UINavigationController is not loaded yet and the frame of self.view is 'perceived' to be (not really, because it is) larger.

This explains the difference of 40.0 (?) points.

One possible solution to this problematic behaviour is to initialize and add (at least the first) instance of UIWebView in the viewWillAppear:animated method. (UIViewController does already implement this method)

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