如何在 UIWebView 中定位 UIToolbar
我有一个示例项目,其中创建了一个名为 WebView 的自定义 UIWebView。它们被添加到 UIViewController 中的视图中。两个 WebView 在 viewDidLoad 中初始化并添加到数组列表中。第一个添加为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是,当您通过单击按钮初始化时,viewDidLoad 上的 self.frame.size.height 是不一样的。如果你直接在
viewDidLoad
中添加UIWebView
,你的UINavigationController
的UINavigationBar
还没有加载,框架self.view
被“认为”更大(不是真的,因为它更大)。这就解释了 40.0 (?) 分的差异。
解决此问题行为的一个可能的解决方案是在
viewWillAppear:animated
方法中初始化并添加(至少是第一个)UIWebView
实例。 (UIViewController
已经实现了这个方法)The thing here is that
self.frame.size.height
onviewDidLoad
is not the same when you initialize it via the button click. If you add theUIWebView
directly inviewDidLoad
, theUINavigationBar
of yourUINavigationController
is not loaded yet and the frame ofself.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 theviewWillAppear:animated
method. (UIViewController
does already implement this method)