IOS View仍然无法加载到正确的位置

时间:2014-11-22 22:48:19

标签: ios uiwebview position

我以为我已经清理了但显然没有。我使用下面的代码将webview设置为显示在顶部的nag bar和底部的tab栏之间。这发生在我的viewDidLoad()方法中。它在我测试过的所有模拟器上运行良好,但是当我测试一个运行7.1.1的朋友的iPhone 4时,webview呈现跨越屏幕的整个高度,由顶部唠叨条和底部标签覆盖杆

如何在7以上的所有设备和操作系统中获得所需的行为?

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

[self.view addSubview:self.webView];
self.webView.scalesPageToFit = true;
NSURL *url = [NSURL URLWithString:@"http://example.com/notifications.php"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
self.webView.scrollView.showsVerticalScrollIndicator = false;

self.navigationController.navigationBar.titleTextAttributes = @{UITextAttributeTextColor : [UIColor whiteColor]};

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.webView.delegate = self;
self.webView.scrollView.delegate = self;

1 个答案:

答案 0 :(得分:1)

这是因为你正在跨越整个视图 在你的矩形中你需要减去标签和导航栏的高度和宽度

看一下这个例子

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat viewheight = self.view.frame.size.height;
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
    CGFloat spaceToRemove = navBarHeight + tabBarHeight;
    CGFloat newHeight = viewheight - spaceToRemove;

    NSLog(@"%f",newHeight);
    NSLog(@"%f",self.view.frame.size.height);


  CGRect frame =  CGRectMake(0 , 0 + navBarHeight, self.view.frame.size.width, newHeight);

    UIView *newView = [[UIView alloc]initWithFrame:frame];
    newView.backgroundColor = [UIColor redColor];
    [self.view addSubview:newView];


}