我将当前的基本sdk设置为iOS 4.2。 我有这个方法:
- (void) setNavigationBarBackground
{
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) {
//iOS 5.0
if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self setBackgroundImage:[UIImage imageNamed:kImageNavBar] forBarMetrics:UIBarMetricsDefault];
}
}
else
{
//iOS 4.whatever and below
UIImageView *imageView = (UIImageView *)[self viewWithTag:kNavigationBarBackgroundImageTag];
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kImageNavBar]];
[imageView setTag:kNavigationBarBackgroundImageTag];
[self insertSubview:imageView atIndex:0];
[imageView release];
}
}
}
当我构建项目时,遇到编译时错误:
'UIBarMetrics' Default undeclared (first use in this function)
当我将基本sdk设置为5.0时,它会编译而没有任何错误。
有谁知道如何使用base sdk 4.2编译它?
答案 0 :(得分:4)
特别是你的情况,请使用:
#if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"TitleBar"] forBarMetrics:UIBarMetricsDefault];
}
#endif
通常,在各种SDK版本中有条件地编译的好方法是:
将它放在顶部(连同导入):
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
然后如果有一些特定于操作系统的功能,请使用它们(我使用AlertView作为示例。在iOS5之前,UIAlertView不支持其中的自定义textView,因此我有自己的自定义AlertView。在iOS5中, hack不起作用,我必须使用UIAlertView,因为它支持自定义textViews):
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
TextAlertView *alert = [[TextAlertView alloc] initWithTitle:@"xxxYYzz"
message:@""
delegate:self cancelButtonTitle:@"Add"
otherButtonTitles:@"Cancel", nil];
alert.textField.keyboardType = UIKeyboardTypeDefault;
alert.tag = 1;
self.recipeNameTextField = alert.textField;
[alert show];
[alert release];
}
else {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"xxYYzz"
message:@""
delegate:self cancelButtonTitle:@"Add"
otherButtonTitles:@"Cancel", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
self.recipeNameTextField = [alert textFieldAtIndex:0];
[alert show];
[alert release];
}
希望有所帮助
答案 1 :(得分:0)
将“基本SDK”设置为“最新iOS”。
将“iOS部署目标”设置为您要支持的最低版本,在本例中为“iOS 4.2”。