IOS 视图仍然没有加载到正确的位置 [英] IOS View still does not load in the correct position

查看:21
本文介绍了IOS 视图仍然没有加载到正确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以为我已经解决了这个问题,但显然没有.我正在使用下面的代码将 webview 设置为出现在顶部导航栏和底部标签栏之间.这发生在我的 viewDidLoad() 方法中.它在我测试过的所有模拟器上都运行良好,但是当我测试一个朋友的 iPhone 4s 运行 7.1.1 时,呈现的 web 视图跨越整个屏幕高度,被顶部导航栏和底部标签栏覆盖.

如何在 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]};[超级viewDidLoad];//在加载视图后做任何额外的设置,通常是从笔尖.self.webView.delegate = self;self.webView.scrollView.delegate = self;

解决方案

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

看看这个例子

- (void)viewDidLoad {[超级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];}

I thought I had this cleared up but apparently not. I am using the code below to set a webview to appear between the top nag bar and tab bar at the bottom. This happens in my viewDidLoad() method. It was working fine on all simulators I had tested it on, however when I tested a friend's iPhone 4s running 7.1.1 the webview rendered spanning the entire height of the screen, covered by both the top nag bar and bottom tab bar.

How to I get the desired behavior across all devices and OS above 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;

解决方案

It's because you are spanning the entire view in your rect make you need to subtract the height and width of the tab and nav bar

take a look at this example

- (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];


}

这篇关于IOS 视图仍然没有加载到正确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆