IOS View still does not load in the correct positi

2019-08-23 08:10发布

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;

1条回答
虎瘦雄心在
2楼-- · 2019-08-23 08:39

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


}
查看更多
登录 后发表回答