【问题标题】:UIButton Programmatically based on deviceUIButton 以编程方式基于设备
【发布时间】:2011-05-26 19:30:21
【问题描述】:

我以编程方式创建了一个 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, 25, 24, 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];  

【问题讨论】:

  • 放置 appButton = [[UIButton buttonWithType:UIButtonTypeCustom] 保留]; if之前

标签: ios4 xcode4 uibutton ipad


【解决方案1】:

您可以简单地进行以下操作:

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 语句行
  • 您在 isEqualToString 之后缺少一个“:”。现在,由于某种原因,我的 iPhone 上的按钮不见了,但它是我在 iPad 上指定的位置
  • 对不起“:”,请告诉我你正在使用的代码。
  • 看起来是直接跳到 else 语句了。
  • 放置一个 NSLog(@"Model: %s",[[UIDevice currentDevice] model]);对于 iPhone,可能是“iPhone OS”,或者类似的东西,而不是“iPhone”
【解决方案2】:

@Saimonx 感谢您将我推向正确的方向。我找到了答案,并使用以下方法完成:

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//put your code here
}

else

{
//put your code here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-25
    • 2015-11-20
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多