Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> UIButton的使用

UIButton的使用

編輯:關於Android編程

UIButton是按鈕視圖控件,是UIView的子類,也擁有和view一樣的屬性,同時也有自己特有的屬性;

UIButton的主要作用是用來響應,或控制其他事件的發生;比如說顯示,或隱藏其他控件,或調用其他函數方法。

  1. UILabel*lable01=[[UILabelalloc]initWithFrame:CGRectMake(0.0,0.0,300.0,40.0)];
  2. lable01.backgroundColor=[UIColoryellowColor];
  3. lable01.text=@"按鈕在控制我的顯示,或隱藏";
  4. lable01.textColor=[UIColorredColor];
  5. [self.viewaddSubview:lable01];
  6. lable01.hidden=YES;
  7. lable01.center=self.view.center;
  8. lable01.tag=2000;
  9. //實例化
  10. UIButton*button001=[[UIButtonalloc]initWithFrame:CGRectMake(10.0,50.0,120.0,40.0)];
  11. //是view的子類,擁有view一樣的屬性
  12. [self.viewaddSubview:button001];
  13. //button001.backgroundColor=[UIColoryellowColor];
  14. //button001.layer.cornerRadius=5.0;
  15. //button001.layer.masksToBounds=YES;
  16. //button001.layer.borderWidth=0.5;
  17. //button001.layer.borderColor=[UIColorredColor].CGColor;
  18. button001.tag=1000;
  19. //自己特有的屬性
  20.  
  21. //1選中狀態,默認是NO,即未選中;通常結合響應方法進行使用
  22. button001.selected=NO;
  23. //2按鈕標題顏色,可以根據響應狀態進行設置,比如常規,或高亮,或選中等
  24. [button001setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
  25. [button001setTitleColor:[UIColorblackColor]forState:UIControlStateHighlighted];
  26. [button001setTitleColor:[UIColorgreenColor]forState:UIControlStateSelected];
     
  27. //3按鈕標題
  28. //3-1大小
  29. button001.titleLabel.font=[UIFontsystemFontOfSize:12.0];
  30. //3-2對齊方式,默認居中
  31. //button001.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
  32. //button001.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
  33. //4按鈕標題,同樣也可以根據響應狀態進行設置
  34. [button001setTitle:@"顯示標簽"forState:UIControlStateNormal];
  35. [button001setTitle:@"隱藏標簽"forState:UIControlStateHighlighted];
  36. [button001setTitle:@"隱藏標簽"forState:UIControlStateSelected];
  37. //5設置圖片,默認圖標在左,標題在右,還可通過設置改變顯示樣式,比如圖片在上,標題在下,或圖片在右,標題在左。
  38. [button001setImage:[UIImageimageNamed:@"imageNormal"]forState:UIControlStateNormal];
  39. [button001setImage:[UIImageimageNamed:@"imageSelected"]forState:UIControlStateSelected];
  40. //6設置背景圖片
  41. [button001setBackgroundImage:[UIImageimageNamed:@"bgImageNormal"]forState:UIControlStateNormal];
  42. [button001setBackgroundImage:[UIImageimageNamed:@"bgImageHighlight"]forState:UIControlStateHighlighted];
  43. [button001setBackgroundImage:[UIImageimageNamed:@"bgImageSelected"]forState:UIControlStateSelected];
  44. //7其他屬性
  45. //7-1如當前顯示標題
  46. NSString*title=button001.currentTitle;
  47. NSLog(@"title=%@",title);
  48.  
  49. //7-2點擊屬性,默認為YES,即可點擊
  50. //方法1
  51. //BOOLisEnable=button001.enabled;
  52. //NSLog(@"isEnable=%@",@(isEnable));
  53. //button001.enabled=NO;//設置為不可用
  54. //isEnable=button001.enabled;
  55. //NSLog(@"isEnable=%@",@(isEnable));
  56. //方法2view的userInteractionEnabled屬性,默認為YES,即可點擊
  57. BOOLisEnable=button001.userInteractionEnabled;
  58. NSLog(@"isEnable=%@",@(isEnable));
  59. button001.userInteractionEnabled=NO;//設置為不可用
  60. isEnable=button001.userInteractionEnabled;
  61. NSLog(@"isEnable=%@",@(isEnable));
     
  62. //target方法
  63. [button001addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
  64. -(void)buttonClick:(UIButton*)button
  65. {
  66. button.selected=!button.selected;
  67.  
  68. UILabel*label=(UILabel*)[self.viewviewWithTag:2000];
  69. label.hidden=!button.selected;
  70.  
  71. NSLog(@"按鈕被點擊了");
  72. }
  73.  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved