【问题标题】:IOS View still does not load in the correct positionIOS View 仍然没有加载到正确的位置
【发布时间】:2014-11-22 22:48:18
【问题描述】:

我以为我已经解决了这个问题,但显然没有。我正在使用下面的代码来设置一个 webview 显示在顶部导航栏和底部标签栏之间。这发生在我的 viewDidLoad() 方法中。它在我测试过的所有模拟器上运行良好,但是当我测试运行 7.1.1 的朋友的 iPhone 4s 时,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;

【问题讨论】:

    标签: ios uiwebview position


    【解决方案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];
    
    
    }
    

    【讨论】:

    • 很遗憾,Apple 尚未解决此问题。我的意思是我想不出我会希望我的内容被栏或键盘覆盖的场景。
    • 很遗憾,这并不简单。为什么上面的代码适用于其他设备?我会在晚上结束之前测试这段代码,看看会发生什么。谢谢
    • 我不确定为什么新操作系统会出于某种原因试图涵盖所有内容,这至少可以说是非常令人沮丧的。我希望我能找到一种更简单的方法,但我肯定还没有想到。
    • 所以我对此进行了测试,它似乎不适用于 iPhone 模拟器。我会在早上进行设备测试,但它看起来像这里的图片grosh.co/screen.jpg ...
    • 是的,设备上的行为相同@CSpell ...我想这对最初引发问题的设备有效。认为有办法检查吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多