• iOS-UI控件之UIButton


    ---恢复内容开始---

    UIButton

    • 既可以显示图片,又可以显示文字,还能随时调整内部位置
    • 系统自带尺寸

    storyboard内部调整UIButton属性

    状态

    监听按钮点击事件

    • 凡是继承自UIControl的控件,都可以通过addTarget:...方法来监听事件
      addTarget:(id) action:(SEL) forControlEvents:(UIControlEvents)
      
    //UIControlEvents 的取值
    typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
        UIControlEventTouchDown           = 1 <<  0,      // on all touch downs
        UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)
        UIControlEventTouchDragInside     = 1 <<  2,
        UIControlEventTouchDragOutside    = 1 <<  3,
        UIControlEventTouchDragEnter      = 1 <<  4,
        UIControlEventTouchDragExit       = 1 <<  5,
        UIControlEventTouchUpInside       = 1 <<  6,
        UIControlEventTouchUpOutside      = 1 <<  7,
        UIControlEventTouchCancel         = 1 <<  8,
    
        UIControlEventValueChanged        = 1 << 12,     // sliders, etc.
    
        UIControlEventEditingDidBegin     = 1 << 16,     // UITextField
        UIControlEventEditingChanged      = 1 << 17,
        UIControlEventEditingDidEnd       = 1 << 18,
        UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing
    
        UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events
        UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField
        UIControlEventApplicationReserved = 0x0F000000,  // range available for application use
        UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use
        UIControlEventAllEvents           = 0xFFFFFFFF
    };
    

    设置button内部文字和图片的方法

    - (void)setTitle:(NSString *)title forState:(UIControlState)state;
    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
    - (void)setImage:(UIImage *)image forState:(UIControlState)state;
    - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR;
    

    自定义UIButton

    UIButton内部有两个子控件 UILabelUIImageView

    调整控件内部子控件的frame(两种方式)

    • 实现 titleRectForContentRect 和 imageRectForContentRect方法分别返回子控件的 frame
    - (CGRect)titleRectForContentRect:(CGRect)contentRect;
    - (CGRect)imageRectForContentRect:(CGRect)contentRect;
    
    • 在另一方法中设置子控件的frame
    /**
     * 当前控件的frame发生改变的时候就会调用
     * 这个方法专门用来布局子控件,设置子控件的frame
     */
    - (void)layoutSubviews;
    

    内边距

    // 设置按钮内容的内边距(影响到imageView和titleLabel)
    @property(nonatomic)          UIEdgeInsets contentEdgeInsets;
    // 设置titleLabel的内边距(影响到titleLabel)
    @property(nonatomic)          UIEdgeInsets titleEdgeInsets;
    // 设置imageView的内边距(影响到imageView)
    @property(nonatomic)          UIEdgeInsets imageEdgeInsets;
    

    图片拉伸

    • UIButton 图片的拉伸,大小改变

    • iOS5之前

    // 只拉伸中间的1x1区域
    //传入左上的距离的区域
    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
    
    • iOS5开始
    //传入左上右下不拉伸的区域,默认方式是平铺
    - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
    //resizingMode 拉伸方式的参数
    - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
    
    • 另一种改变图片拉伸方式
      • 在Images.xcassets中选择图片
      • 在slicing属性中选择拉伸范围 
      • 自动计算拉伸范围
  • 相关阅读:
    django基础入门(3)django中模板
    ms sql 索引(一)
    Ruby入门(3)——方法、代码段
    Ruby入门(2)——基本流程控制
    Ruby入门(4)——类
    Ruby入门(1)——数据类型
    django基础入门(1)django基本配置
    四则运算加强版
    结对 四则运算
    chrome设置以及hosts备份
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/4603299.html
Copyright © 2020-2023  润新知