UIButton基础知识

时间:2023-03-09 20:10:44
UIButton基础知识

基本属性

  1.frame;坐标;title;titlecolor;字体颜色;titleShadowColor:字体阴影;image:图片; backgroundImage:背景图片;

  2.forstate状态:这个参数决定了标题、图像或其他属性将在何种状态下显现

    UIControlStateNormal:常态

    UIControlStateHightlighted: is set 高亮

    UIControlStateDisabled:禁用

    UIControlStateSelected:中

    UIControlStateApplicationg:plication use 当应用程序标志使用时

    UIControlStateReserview;为内部框架预留的

  3.当按钮高亮或者禁用,UIButton类可以调整自己的外观,下面几个属性可以让你按照需要对按钮的外观进行微调

      adjustsImageWhenHightlighted:默认情况下,在按钮被禁用时,图像会被画的颜色深些

      adjustsImageWhendisabled:默认情况下,按钮在被禁用时,图像会被画的颜色淡一些

      showsTouchWhenHightlighted;这个属性设置为YES,可使按钮在按下时发光

重写绘制行为

  backgroundRectForBounds:指定背景边界

  contentRectForBounds:指定内容边界

  titleRectForContentRent:指定文字标题边界

  imageRectForContentRect;指定按钮图像边界

添加动作

  -addTarget;action:forControlEvents:

  

初始化UIButton控件显示在屏幕上

  UIButton *button = [[UIbutton alloc] initWithFrame:CGRectMake(10,10,10,10)];

  [button setBackgroundColor: [UIColor blueColor]];

  [button setTitle:@"view" forState :UIControlStateNormal];

  [self.view addSubview :button];

  2.UIButton的一个类方法ButtonWithType

  UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];

  [button setFrame:CGRectMake(10,10,10,10)];

  [button setBackgroundColor: [UIColor blueColor]];

  [button setTitle: @"view" forState :UIControlStateNormal];

Button的响应事件

  [button addTarget:self action:@selector(haoshuaihaofan:) forControlEvents:UIControlEventTouchUpInside];

  [self.view addSubview: button];