如何解决UIButton以编程方式基于设备
| 我已经以编程方式创建了一个UIButton,它可以插入到仅iPhone或iPad的应用程序中。是否可以针对通用应用程序执行此操作(即...更改按钮的大小和位置以适合设备UI)?//insert button for inbox///////////////////////////////////////////////////////////
appButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[appButton setBackgroundImage:[UIImage imageNamed:@\"18-envelope.png\"] forState:UIControlStateNormal];
appButton.layer.borderColor=[UIColor clearColor].CGColor;
appButton.backgroundColor = [UIColor whiteColor];
appButton.frame = CGRectMake(290.0,25.0,24.0,24.0);
[appButton addTarget:self
action:@selector(showAppInbox)
forControlEvents:UIControlEventTouchDown];
[self.window addSubview:appButton];
//END/////////////////////////////////
//Call method to show inbox
-(void) showAppInbox
{
[[AppInboxManager sharedManager] show];
}
//新代码
//用于INBOX的BEGIN插入按钮/////////////////////////////////////////// ////////////////
if ([[[UIDevice currentDevice] model] isEqualToString:@\"iPhone\"])
{
appButton.frame = CGRectMake(200,25,24,16);
}
else
{
appButton.frame = CGRectMake(700,16);
}
appButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[appButton setBackgroundImage:[UIImage imageNamed:@\"envelope_white.png\"] forState:UIControlStateNormal];
appButton.layer.borderColor=[UIColor clearColor].CGColor;
appButton.backgroundColor = [UIColor clearColor];
[appButton addTarget:self
action:@selector(showAppInbox)
forControlEvents:UIControlEventTouchDown];
解决方法
您可以简单地进行以下操作:
if ([[[UIDevice currentDevice] model] isEqualToString: @\"iPhone\"]){
appButton.frame=XXXX;
}else{
appButton.frame=YYYY;
}
更多信息:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
,@Saimonx感谢您将我推向正确的方向。我找到了答案,并使用以下命令完成了该操作:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//put your code here
}
else
{
//put your code here
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。