UIView 和 UIButton的简单用法

UIVIew:
1、UIView的概念:ide

UIView表示屏幕上的一块矩形区域,它在App中占有绝对重要的地位,由于IOS中几乎全部可视化控件都是UIView的子类。负责渲染区域的内容,而且响应该区域内发生的触摸事件
 UIView的功能 1.管理矩形区域里的内容2.处理矩形区域中的事件3.子视图的管理 4.还能实现动画  UIView的子类也具备这些功能 学习


2、UIView的基本用法:
一、UIView的初始化:
     UIView *view = [[UIView alloc]initWithFrame:CGRectMack(x,y,width,height)];   字体

其中:CGRect :CGPoint (x,y) CGSize:(width,height)
           CGFloat :单门给UI界面用的float类型。动画

           例如:CGRectMake(10, 10, 200, 200);
二、UIView的相关代码:UIColor :
初始化(固定生成方式):UIColor *color = [UIColor redColor] ;
三原色方式: UIColor *color2 = [UIColor colorWithRed:0.3 green:0.2 blue:0.1 alpha:1]; 
alpha :透明度;orm

RGB:red、green、biue 三原色 范围0-1;
三、管理view层次的方法对象

    view - view2,view3教程

    bringSubviewToFront - 把某个子界面放到最前事件

    [self.view1 bringSubviewToFront:view2];图片

    sendSubviewToBack - 把某个子界面放到最后ip

    [self.view1 sendSubviewToBack:view2];

    inster - 按照index调整位置

    [view insertSubview:view3 atIndex:0];

 裁剪子view到父view大小

    self.view1.clipsToBounds = YES;

UIButton(按钮):
 1、UIButton的概念:
UIButton按钮是IOS开发中最经常使用的控件,做为IOS基础学习教程知识 ,初学者须要了解其基本定义和经常使用设置,以便在开发在熟练运用。 
2、UIButton的基本用法:
一、UIButton的初始化:

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
           UIButtonTypeCustom  默认色是白色,记得设置颜色

     其余的初始化方法:
           UIButtonTypeCustom  - 自定义

           UIButtonTypeSystem  - 系统自带样式

           UIButtonTypeDetailDisclosure - 小叹号

           UIButtonTypeContactAdd -  小加号
二、UIbutton的设置:
 (1)设置标题

     UIControlStateNormal  - 正常状态

     UIControlStateHighlighted  - 点击中ing

     UIControlStateDisabled   -  不可点击

     UIControlStateSelected    -   已选中状态

   
    设置状态就是一种提早方案

    [btn setTitle:@"我是按钮" forState:UIControlStateNormal];

    [btn setTitle:@“按钮是谁" forState:UIControlStateHighlighted];

    

更改不可选状态

    enabled - 是否容许用户点击

    btn.enabled = YES;

(2)设置按钮字体大小和颜色 

    btn.titleLabel.font = [UIFont systemFontOfSize:20];

    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

 

   设定按钮大小位置

    btn.frame = CGRectMake(100, 100, 100, 100);
(3)设置点击事件

     给button添加一个点击事件

     addTarget - 响应的对象

     action - 具体的响应方法 @selector()

     forControlEvents - UIControlEventTouchUpInside 点击了按钮的正中央,在中间抬起来

    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btn];

}

:(UIButton *)btn 参数能够不写,通常加上

- (void)clickBtn:(UIButton *)btn

{

。。。
}

(4)用图片当Button的背景
UIImage *image_normal = [UIImage imageNamed:@"buttongreen"];

      setImage forState :在什么状态下显示什么图片

      [btn setImage:image_normal forState:UIControlStateNormal];

    

    UIImage *image_hi = [UIImage imageNamed:@"buttongreen_highlighted"];

      [btn setImage:image_hi forState:UIControlStateHighlighted];

    

    [btn setTitle:@"我是按钮" forState:UIControlStateNormal];

    [btn setTitle:@"按钮是谁" forState:UIControlStateHighlighted];

    

        setImage - setTitle :

        setBackgroundImage  :图片当作背景,文字在上面

    [btn setBackgroundImage:image_normal forState:UIControlStateNormal];

    [btn setBackgroundImage:image_hi forState:UIControlStateHighlighted];

相关文章
相关标签/搜索