簡體   English   中英

iOS View仍無法正確加載

[英]IOS View still does not load in the correct position

我以為我已經解決了這個問題,但顯然沒有。 我正在使用下面的代碼來設置Web視圖,使其顯示在頂部的導航欄和底部的選項卡欄之間。 這發生在我的viewDidLoad()方法中。 在我測試過的所有模擬器上,它都運行良好,但是當我測試朋友的運行7.1.1的iPhone 4s時,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]};

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

這是因為您要在rect中跨越整個視圖,因此需要減去制表符和導航欄的高度和寬度

看這個例子

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


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM