UIButton 控件使用

openkk 12年前發布 | 26K 次閱讀 IOS Android開發 移動開發

    //login button
// .h 中定義
UIButton _loginBtn;
@property (strong,nonatomic)UIButton
loginBtn;

    // .m 中實現設置按鈕  
    @synthesize loginBtn = _loginBtn;//使用備份變量名  

    //設置按鈕的  形狀  
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    /* 
     buttonWithType:  定義button按鈕的外形 
     六種定義button類型: 下面有圖解 
     UIButtonTypeCustom = 0,    無類型 
     UIButtonTypeRoundedRect,    四個角是圓弧   型的 
     UIButtonTypeDetailDisclosure, 
     UIButtonTypeInfoLight, 
     UIButtonTypeInfoDark, 
     UIButtonTypeContactAdd, 
     */  

    //定義button按鈕在frame上的坐標(位置),和這個按鈕的寬/高  
    self.loginBtn.frame = CGRectMake(40, 200, 80, 30);  


    [self.loginBtn setTitle:@"Login" forState:UIControlStateNormal];  
    /* 
     常用的屬性: 
      setTitle:  設置button按鈕的名稱 
      setImage: [UIImage imageNamed:@"圖名"]  添加圖片 
      setTitleColor:[UIColor redColor]  設置字體顏色 

     forState 設置 按鈕點擊前后的狀態   : 下有圖解 
     UIControlStateHighlighted 
     UIControlStateSelected 
     UIControlStateDisabled 
     UIControlStateNormal 

     */  

    // 為按鈕添加一個動作  
    //  action:  如果點擊的話執行的方法  
    [self.loginBtn addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside];  

    //把button控件添加到view中顯示  
    [self.view addSubview:self.loginBtn];  </pre><pre class="brush:cpp; toolbar: true; auto-links: false;">    //執行動作的方法  
-(IBAction)Login:(id)sender;  </pre><br />

六種定義button類型: 

     UIButtonTypeCustom = 0,   無類型

     UIButtonTypeRoundedRect,   四個角是圓弧  型的   UIButton 控件


     UIButtonTypeDetailDisclosure    UIButton 控件

     UIButtonTypeInfoLight    UIButton 控件

     UIButtonTypeInfoDark    UIButton 控件

     UIButtonTypeContactAdd    UIButton 控件

forState 設置 按鈕點擊前后的狀態   

        點擊前                                         點擊后

UIButton 控件 UIControlStateHighlightedUIButton 控件

UIButton 控件 UIControlStateSelected   UIButton 控件

UIButton 控件 UIControlStateDisabled   UIButton 控件

  UIButton 控件     UIControlStateNormal     UIButton 控件


轉自:http://blog.csdn.net/like7xiaoben/article/details/7588551

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!