2016-09-25 66 views
0

我有一個爲iphone 4或4s構建的舊應用程序。 我想稍微改變它,並將其作爲適用於包括iPad在內的所有設備的應用發佈。 我已經調整了代碼,所以現在它在所有設備上工作,但是當我在iPhone上運行它時,4s的應用會在頂部和底部切割。 問題是應用程序的用戶界面是用代碼編寫的,根本不使用StoryBoard。 的UI代碼:如何使iPhone 4應用適合所有iOS尺寸?

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.screenHeight = [UIScreen mainScreen].bounds.size.height; 

UIImage *background = [UIImage imageNamed:@"bg"]; 

self.backgroundImageView = [[UIImageView alloc] initWithImage:background]; 
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill; 
[self.view addSubview:self.backgroundImageView]; 

self.blurredImageView = [[UIImageView alloc] init]; 
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill; 
self.blurredImageView.alpha = 0; 
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil]; 
[self.view addSubview:self.blurredImageView]; 

self.tableView = [[UITableView alloc] init]; 
self.tableView.backgroundColor = [UIColor clearColor]; 
self.tableView.delegate = self; 
self.tableView.dataSource = self; 
self.tableView.separatorColor = [UIColor colorWithWhite:1 alpha:0.2]; 
self.tableView.pagingEnabled = YES; 
[self.view addSubview:self.tableView]; 

CGRect headerFrame = [UIScreen mainScreen].bounds; 
CGFloat inset = 20; 
CGFloat temperatureHeight = 110; 
CGFloat hiloHeight = 40; 
CGFloat iconHeight = 30; 
CGRect hiloFrame = CGRectMake(inset, headerFrame.size.height - hiloHeight, headerFrame.size.width - 2*inset, hiloHeight); 
CGRect temperatureFrame = CGRectMake(inset, headerFrame.size.height - temperatureHeight - hiloHeight, headerFrame.size.width - 2*inset, temperatureHeight); 
CGRect iconFrame = CGRectMake(inset, temperatureFrame.origin.y - iconHeight, iconHeight, iconHeight); 
CGRect conditionsFrame = iconFrame; 
// make the conditions text a little smaller than the view 
// and to the right of our icon 
conditionsFrame.size.width = self.view.bounds.size.width - 2*inset - iconHeight - 10; 
conditionsFrame.origin.x = iconFrame.origin.x + iconHeight + 10; 

UIView *header = [[UIView alloc] initWithFrame:headerFrame]; 
header.backgroundColor = [UIColor clearColor]; 
self.tableView.tableHeaderView = header; 

// bottom left 
UILabel *temperatureLabel = [[UILabel alloc] initWithFrame:temperatureFrame]; 
temperatureLabel.backgroundColor = [UIColor clearColor]; 
temperatureLabel.textColor = [UIColor whiteColor]; 
temperatureLabel.text = @"0°"; 
temperatureLabel.font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:120]; 
[header addSubview:temperatureLabel]; 

// bottom left 
UILabel *hiloLabel = [[UILabel alloc] initWithFrame:hiloFrame]; 
hiloLabel.backgroundColor = [UIColor clearColor]; 
hiloLabel.textColor = [UIColor whiteColor]; 
hiloLabel.text = @"0°/0°"; 
hiloLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:28]; 
[header addSubview:hiloLabel]; 

// top 
UILabel *cityLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 30)]; 
cityLabel.backgroundColor = [UIColor clearColor]; 
cityLabel.textColor = [UIColor whiteColor]; 
cityLabel.text = @"Loading..."; 
cityLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18]; 
cityLabel.textAlignment = NSTextAlignmentCenter; 
[header addSubview:cityLabel]; 

UILabel *conditionsLabel = [[UILabel alloc] initWithFrame:conditionsFrame]; 
conditionsLabel.backgroundColor = [UIColor clearColor]; 
conditionsLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18]; 
conditionsLabel.textColor = [UIColor whiteColor]; 
[header addSubview:conditionsLabel]; 

// bottom left 
UIImageView *iconView = [[UIImageView alloc] initWithFrame:iconFrame]; 
iconView.contentMode = UIViewContentModeScaleAspectFit; 
iconView.backgroundColor = [UIColor clearColor]; 
[header addSubview:iconView]; 

[[RACObserve([WXManager sharedManager], currentCondition) 
    deliverOn:RACScheduler.mainThreadScheduler] 
subscribeNext:^(WXCondition *newCondition) { 
    temperatureLabel.text = [NSString stringWithFormat:@"%.0f°",newCondition.temperature.floatValue]; 
    conditionsLabel.text = [newCondition.condition capitalizedString]; 
    cityLabel.text = [newCondition.locationName capitalizedString]; 

    iconView.image = [UIImage imageNamed:[newCondition imageName]]; 
}]; 

RAC(hiloLabel, text) = [[RACSignal combineLatest:@[ 
                RACObserve([WXManager sharedManager], currentCondition.tempHigh), 
                RACObserve([WXManager sharedManager], currentCondition.tempLow)] 
              reduce:^(NSNumber *hi, NSNumber *low) { 
               return [NSString stringWithFormat:@"%.0f°/%.0f°",hi.floatValue,low.floatValue]; 
              }] 
         deliverOn:RACScheduler.mainThreadScheduler]; 

[[RACObserve([WXManager sharedManager], hourlyForecast) 
    deliverOn:RACScheduler.mainThreadScheduler] 
subscribeNext:^(NSArray *newForecast) { 
    [self.tableView reloadData]; 
}]; 

[[RACObserve([WXManager sharedManager], dailyForecast) 
    deliverOn:RACScheduler.mainThreadScheduler] 
subscribeNext:^(NSArray *newForecast) { 
    [self.tableView reloadData]; 
}]; 

[[WXManager sharedManager] findCurrentLocation]; 

}

希望你能幫助我,在此先感謝!

回答

1

應用程序被切斷的頂部和底部

您必須使用適當大小的屏幕上推出的所有器件尺寸。現在最簡單的方法就是LaunchScreen故事板。製作一個新項目,你會看到它是如何配置的。

+0

嗨,感謝您的回覆。 我添加了一個LaunchScreen.storyboard文件到我的項目中,我剛從一個新的空白項目中複製它。屏幕仍然被切斷。我現在能做什麼?我需要改變一些東西才能使它起作用嗎? –

+0

我說過,看看它是如何配置的。如果你沒有看到如何,最好把你的代碼複製到一個新的項目中。新項目將自動使用啓動畫面故事板。 – matt

+0

問題是,該項目正在使用大量的cocoapods框架..我試圖將其複製到一個新的項目,但它不工作。我從一個新項目中複製了LaunchScreen.storyboard,並試圖查看這個文件和另一個新文件是否有不同,並且沒有看到任何區別。我能做什麼? –

相關問題