在ios开发中,我们必须以编程方式创建接口或根据某些条件生成接口。我想知道如何在不使用自动布局的情况下以编程方式支持多种屏幕尺寸。
答案 0 :(得分:2)
您可以执行以下操作
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) {
if (screenSize.height > 480.0f)
{
//Do iphone5 Stuff here
}
else
{
//Do iphone classic Stuff here
}
}
else
{
//Do ipad Stuff here
}
答案 1 :(得分:2)
您可以使用某些宏
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f)
#define IS_IOS7 ((floor(NSFoundationVersionNumber)>NSFoundationVersionNumber_iOS_6_1))
#define IS_LANDSCAPE ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
将接口上的所有宏放在.h中并开始在.m文件中使用
您可以使用此
签入任何视图控制器-(void)createControls{
if(IS_IPHONE){
//Do iphone stuff
}
else{
//Do ipad stuff
}
}
答案 2 :(得分:1)
+(NSString *)yesButWhichDeviceIsIt
{
BOOL hasRetina = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0) {
hasRetina = YES;
}
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (hasRetina) {
return @"iPad retina";
} else {
return @"iPad";
}
} else {
if (hasRetina) {
if ([[UIScreen mainScreen] bounds].size.height == 568){
return @"iPhone5";
}
else
{
return @"iPhone4s";
}
} else {
return @"iPhone";
}
}
}
通过使用上述方法,您将获得设备类型,基于您可以编码的结果:
NSString *deviceTypeString=[self yesButWhichDeviceIsIt];
if([deviceTypeString isEqualToString: @"iPhone5"])
{
}
答案 3 :(得分:0)
-(NSString *)whichiOSDevice
{
BOOL hasRetina = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0) {
hasRetina = YES;
}
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (hasRetina) {
return @"iPad retina";
} else {
return @"iPad";
}
} else {
if (hasRetina) {
if ([[UIScreen mainScreen] bounds].size.height == 568){
return @"iPhone5";
}
else
{
return @"iPhone4s";
}
} else {
return @"iPhone";
}
}
}