zoukankan      html  css  js  c++  java
  • 自定义UIButton及注意点

    一.自定义UIButton

    /*

    调整Button内部子控件的步骤

            1.自定义Button

            2.调整位置

                1>重写两个方法:titleRectForContentRect:和imageRectForContentRect:

                2>重写layoutSubviews: 先调用super方法.之后自己调整frame

            3.如果需要设置imageView和titleLabel的属性时,在initWithFrame:方法设置

         */

        

        // 1.创建UIButton对象

        XMGButton *btn = [XMGButton buttonWithType:UIButtonTypeCustom];

        

        // 2.设置frame

        btn.frame = CGRectMake(100, 100, 175, 50);

        

        // 3.设置背景颜色

        [btn setBackgroundColor:[UIColor purpleColor]];

        

        // 4.设置btn显示的文字

        [btn setTitle:@"普通按钮" forState:UIControlStateNormal];

        

        // 5.设置btn显示的图片

        [btn setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_normal"] forState:UIControlStateNormal];

        

        // 添加到控制器的View中

        [self.view addSubview:btn];

        

        // 6.设置btn中titleLabel/imageView的背景颜色

        btn.titleLabel.backgroundColor = [UIColor blueColor];

        btn.imageView.backgroundColor = [UIColor orangeColor];

        

        // 7.调整titleLabel/imageView的位置

        // 注意:不管外界如何调整btn的titleLabel/imageView的位置,button内部都会调整回去

        /*

        btn.titleLabel.frame = CGRectMake(0, 0, 120, 50);

        btn.imageView.frame = CGRectMake(120, 0, 55, 50);

         */

        /*

        btn.titleLabel.textAlignment = NSTextAlignmentCenter;

        btn.imageView.contentMode = UIViewContentModeCenter;

         */

    二.UIButton的内边距属性

     self.btn.contentEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);

        self.btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10);

        self.btn.imageEdgeInsets = UIEdgeInsetsMake(2, 0, 0, 10);

  • 相关阅读:
    Linux kernel device mapper
    草莓网
    openwrt系统源码地址
    ubuntu 安装eclipse for c++
    ubuntu下安装eclipse IDE for C/C++ developers
    Makefile 中:= ?= += =的区别
    core dump
    rtp
    skbuff
    A Neural Algorithm of Artistic Style
  • 原文地址:https://www.cnblogs.com/wxdonly/p/5097181.html
Copyright © 2011-2022 走看看