【问题标题】:How do I create a basic UIButton programmatically?如何以编程方式创建基本的 UIButton?
【发布时间】:2010-11-25 14:41:49
【问题描述】:

如何以编程方式创建基本的UIButton?例如在我的视图控制器中,在执行viewDidLoad方法时,会动态创建三个UIButtons,并设置其布局或属性。

【问题讨论】:

标签: ios objective-c cocoa-touch uibutton programmatically-created


【解决方案1】:
UIButton * tmpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tmpBtn addTarget:self action:@selector(clearCart:) forControlEvents:UIControlEventTouchUpInside];
tmpBtn.tag = k;
tmpBtn.frame = CGRectMake(45, 0, 15, 15);
[tmpBtn setBackgroundImage:[UIImage imageNamed:@"CloseButton.png"] forState:UIControlStateNormal];
[self.view addSubview:tmpBtn];

【讨论】:

    【解决方案2】:
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:@"Click" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    
    button.frame=CGRectMake(self.view.frame.origin.x+10,self.view.frame.size.height/2, self.view.frame.size.width-20, self.view.frame.size.height-500);
    
     button.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:20];
    
    [button addTarget:selfaction:@selector(aMethod)forControlEvents:UIControlEventTouchUpInside];
    
    [button setBackgroundColor:[UIColor grayColor]];
    
    [self.view addSubview:button];
    

    【讨论】:

      【解决方案3】:
      UIButton *saveLibrary=[UIButton buttonWithType:UIButtonTypeCustom];
      
      [saveLibrary setTitle:@"Library" forState:UIControlStateNormal];
      
      [saveLibrary setBackgroundColor:[UIColor redColor]];
      
      [saveLibrary addTarget:self 
      action:@selector(saveOnGalleryButtonIsPressed) forControlEvents:UIControlEventTouchUpInside];
      
      [saveLibrary setImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/library225.png"]]forState:UIControlStateNormal];
      
      saveLibrary.frame=CGRectMake(323, 15, 75, 75);  
      
      [self.view addSubview:saveLibrary];
      

      【讨论】:

        【解决方案4】:
        UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
        addProject.frame = CGRectMake(210, 285, 100, 18);
        [addProject setTitle:@"Show View" forState:UIControlStateNormal];
        [addProject addTarget:self action:@selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:addProject];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多