• iOS UIView控件的常用属性和方法的总结


    一 UIVIew 常见属性
    1.frame 位置和尺寸(以父控件的左上角为原点(0,0))
    2.center 中点 (以父控件的左上角为原点(0,0))
    3.bounds 位置和尺寸(以自己的左上角为原点 (0,0))
    4.transform 形变属性(缩放,旋转)
    5.backgroundColor 背景颜色
    6.tag 标识(父控件可以根据这个标识找到对应的子控件,同一个父控件中的子控件不要一样)
    7. hidden 设置是否要隐藏
    8.alpha 透明度(0~1);
    9.opaque 不透明度(0~1);
    10.userInteractionEnabled 能否跟用户进行交互(YES 能交互)
    11.superView 父控件
    12.subviews 子控件
    13.contentMode 内容显示的模式 拉伸自适应

    [view viewWithTag:10];
    [
    btn1 9
    btn2 10
    imageView 2 10
    ]

    二.UIView常见方法
    1.addSubview
    添加子控件,被添加到最上面(subviews中的最后面)

    2.removeFromSuperview
    从父控件中移除

    3.viewWithTag:
    父控件可以根据这个tag 标识找到对应的控件(遍历所有的子控件)

    4.insertSubview:atIndex:
    添加子控件到指定的位置

    5.利用两个类方法来执行动画的两个方法
    +(void) beginAnimations:(NSString *)animationID context:(void *)context;
    /**..需要执行动画的代码..**/
    +(void) commitAnimations;

    6.利用blok 执行动画
    /*
    duration 动画持续时间
    animations 存放需林执行动画的代码
    completion 存放动画完毕后需要执行的操作代码
    */
    + (void) animateWithDuration:(NSTimeInterval) duration animations:(void (^)(void))animations completion:(void(^)) (BOOL finished) completion

    7.改变view上图层顺序

    - (void)removeFromSuperview;//移除图层(控件)

    - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;//在某个位置插入图层

    - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;//将两个不同位置的图层进行交换

    - (void)addSubview:(UIView *)view;//添加图层在最上面

    - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;//在某一图层的下面插入一个图层(某一图层必须是他的兄弟视图,即必须是在一个父视图上)

    - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;//在某一个图层的上面插入一个图层(同上)

    - (void)bringSubviewToFront:(UIView *)view;//将某一个控件提到最上面

    - (void)sendSubviewToBack:(UIView *)view;//将某一个控件提到最下面

    /*

    下面这两个是典型的协议方法,你可以自定义类在里面重写他们的方法体。当控件执行相应操作时候,让它执行。

    */

    - (void)didAddSubview:(UIView *)subview;//已经添加了这个控件调用这个方法

    - (void)willRemoveSubview:(UIView *)subview;//即将移除这个控件调用这个方法


    三.UIControl
    1.只要继承UIControl ,就能简单地处理事件(点击事件,值改变事件)

    2.继承了UIControl的子类
    UIButton.UISlider.UISwitch.UIDatePicker 等等

    3.当需要监听了一个子控件时间的时候,解决步骤:

    1>.先看它是否继承自UIControl
    2>.再看它内部是否有delegate属性

    4.常用属性

    1>enabled 能否处理时间
    2>contentVerticalAlignment 内容在垂直方向上的排布方式
    3>contentHorizontalAlignment 内容在水平方向上的排布方式
    5.常用方法
    1> 添加监听器
    /*
    target 监听器对象
    action 事件触发时所调用的方法,调用target的方法
    */
    -(void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

    2> 删除监听器
    //删除监听器后,事件触发时就不会再通知监听器了,也就不会再调用target的action方法了
    -(void roemoveTarget:(id)target action:](SEL)action forControlEvents:](UIControlEvents) controlEvents);

    3> 获得所有的监听器对象
    -(NSSet *) allTargets;

    四,UILabel
    1.常见属性
    1>text 所显示的文本内容
    2>textColor 文本颜色
    3> font 字体
    4> shadowColor 文字的阴影颜色
    5> shadowOffset 阴影的偏差距离(width水平方向的偏差距离,height垂直方向的念头距离,正数下边)
    6> textAlignment 设置文字的排布方法(偏左,偏右,居中).
    7>numberOfLines 允许文字最多有几行数(如果为0,自动换行).
    五.UIButton
    //.UISlider .UISwitch .UIDatePicker等等

    (1)UIslider滑块控件在IOS开发中会常用到,可用于调节音量,字体大小等UI方面的交互,用法总结如下:

    初始化一个滑块:

    1
     UISlider * slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    设置滑块位置

    @property(nonatomic) float value; 
    这个值是介于滑块的最大值和最小值之间的,如果没有设置边界值,默认为0-1;

    设置滑块最小边界值(默认为0)

    @property(nonatomic) float minimumValue;  

    设置滑块最大边界值(默认为1)

    @property(nonatomic) float maximumValue; 

    设置滑块最左端显示的图片:

    @property(nonatomic,retain) UIImage *minimumValueImage;

    设置滑块最右端显示的图片:

    @property(nonatomic,retain) UIImage *maximumValueImage; 

    设置滑块值是否连续变化(默认为YES)

    @property(nonatomic,getter=isContinuous) BOOL continuous; 

    这个属性设置为YES则在滑动时,其value就会随时变化,设置为NO,则当滑动结束时,value才会改变。 

    设置滑块左边(小于部分)线条的颜色

    @property(nonatomic,retain) UIColor *minimumTrackTintColor;

    设置滑块右边(大于部分)线条的颜色

    @property(nonatomic,retain) UIColor *maximumTrackTintColor;

    设置滑块颜色(影响已划过一端的颜色)

    @property(nonatomic,retain) UIColor *thumbTintColor;

    注意这个属性:如果你没有设置滑块的图片,那个这个属性将只会改变已划过一段线条的颜色,不会改变滑块的颜色,如果你设置了滑块的图片,又设置了这个属性,那么滑块的图片将不显示,滑块的颜色会改变(IOS7)

    手动设置滑块的值:

    - (void)setValue:(float)value animated:(BOOL)animated;

    设置滑块的图片:

    - (void)setThumbImage:(UIImage *)image forState:(UIControlState)state;

    设置滑块划过部分的线条图案

    - (void)setMinimumTrackImage:(UIImage *)image forState:(UIControlState)state;

    设置滑块未划过部分的线条图案

    - (void)setMaximumTrackImage:(UIImage *)image forState:(UIControlState)state;

    对应的几个get方法

    - (UIImage *)thumbImageForState:(UIControlState)state;
    - (UIImage *)minimumTrackImageForState:(UIControlState)state;
    - (UIImage *)maximumTrackImageForState:(UIControlState)state;

    对应的设置当前状态的响应属性的方法

    @property(nonatomic,readonly) UIImage* currentThumbImage;
    @property(nonatomic,readonly) UIImage* currentMinimumTrackImage;
    @property(nonatomic,readonly) UIImage* currentMaximumTrackImage;

    添加触发事件

    1
    [slider addTarget:self action:@selector(log:) forControlEvents:UIControlEventValueChanged];

    (2)UISwitch简单使用总结:

    初始化:该控件起名的时候千万注意不要用关键词 ,switch。否则会出现不能初始化的问题。

    - (instancetype)initWithFrame:(CGRect)frame; 

    这个frame是没有意义的,系统的开关控件大小是确定的。

    设置开关开启状态时的颜色

    @property(nonatomic, retain) UIColor *onTintColor;

    设置开关风格颜色

    @property(nonatomic, retain) UIColor *tintColor;

    设置开关按钮颜色

    @property(nonatomic, retain) UIColor *thumbTintColor;

    设置开关开启状态时的图片(注意:在IOS7后不再起任何作用)

    @property(nonatomic, retain) UIImage *onImage;

    设置开关关闭状态时的图片(注意:在IOS7后不再起任何作用)

    @property(nonatomic, retain) UIImage *offImage;

    开关的状态

    @property(nonatomic,getter=isOn) BOOL on;

    手动设置开关状态

    - (void)setOn:(BOOL)on animated:(BOOL)animated; 

    一点感想:IOS的系统的UISwitch控件虽然定制性很差,配合IOS7之后的扁平化和俭约的风格,在美观上确实不逊色于任何私人定制的开关控件,在没有特殊需求的情况下,对于开关逻辑,这是一个非常不错的UI交互选择。

    (3)UIDatePicker

    UIPickerView是iOS中的原生选择器控件,使用方便,用法简单,效果漂亮。数据源代理必须设置,没有数据源方法的执行给DatePicker赋行和列数。则该控件就显示不出来,并且崩溃。

    @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;                

    @property(nonatomic,assign) id<UIPickerViewDelegate>   delegate; 

    设置数据源和代理

    @property(nonatomic) BOOL showsSelectionIndicator;

    是否显示选择框,在iOS7之后这个属性没有任何效果

    @property(nonatomic,readonly) NSInteger numberOfComponents;

    获取分区数

    - (NSInteger)numberOfRowsInComponent:(NSInteger)component;

    获取某一分区的行数

    - (CGSize)rowSizeForComponent:(NSInteger)component;

    获取某一分区行的尺寸

    - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;

    获取某一分区某一行的视图

    - (void)reloadAllComponents;

    重载所有分区

    - (void)reloadComponent:(NSInteger)component;

    重载某一分区

    - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; 

    设置选中某一分区某一行

    - (NSInteger)selectedRowInComponent:(NSInteger)component;  

    返回某一分区选中的行

    数据源代理中的方法:

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

    设置分区数

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

    根据分区设置行数

    代理中的方法:

     - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

    设置分区宽度

     - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

    设置分区行高

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

    设置某一行显示的标题

    - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component;

    通过属性字符串设置某一行显示的标题

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

    设置某一行显示的view视图

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

    选中某一行时执行的回调


    1.常见属性

    1>titleLabel 获取内部的UILabel 对象
    2>imageView 获取内部的UIImageView对象

    2.常见方法
    1>设置内部UILabel 显示的文本内容
    //设置按钮文本的时候不能 btn .titleLabel.text = @"4324324";
    - (void)setTitle:(NSString *)title forState:(UIControlState)state; // default is nil. title is assumed to be single line

    2> 设置内部UILabel的文字颜色
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white
    3>设置内部UILabel 的文字阴影颜色
    - (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black
    4>设置内部UIImageView的图片
    - (void)setImage:(UIImage *)image forState:(UIControlState)state; // default is nil. should be same size if different for different states
    5>设置内部UIImageView的图片
    - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil

    6>下面两个方法需要交给子类去重写
    //返回内部UILabel的frame (位置和尺寸)
    -(CGRect)titleRectForContentRect:(CGRect)contentRect;
    //返回内部UIImage的尺寸和位置
    -(CGRect)imageRectForContentRect:(CGRect) contentRect;

    7> 下面这些方法可以获取不同状态下的一些属性
    - (NSString *)titleForState:(UIControlState)state; // these getters only take a single state value
    - (UIColor *)titleColorForState:(UIControlState)state;
    - (UIColor *)titleShadowColorForState:(UIControlState)state;
    - (UIImage *)imageForState:(UIControlState)state;
    - (UIImage *)backgroundImageForState:(UIControlState)state;
    - (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

  • 相关阅读:
    Lambada. 计算和
    adb server version (31) doesn't match this client (39) 解决方案
    python爬虫beautifulsoup4系列2
    python爬虫beautifulsoup4系列1
    利用Python攻破12306的最后一道防线
    python自动化17-JS处理滚动条
    python多线程
    python接口自动化2-发送post请求
    python接口自动化1-发送get请求 前言
    jenkens其实是代码上传工具
  • 原文地址:https://www.cnblogs.com/fuunnyy/p/4712864.html
Copyright © 2020-2023  润新知