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);

  • 相关阅读:
    UIScrollView设置滑动的距离
    iOS动画效果和实现
    cmd 编码 utf8
    详细的ifcfg-eth0配置详解
    Centos下 Nginx安装与配置
    linux安装目录
    utf8 和 UTF-8 的区别
    后台添加搜索功能讲解
    移动端 禁横向滚动
    PC端 $_SERVER 说明
  • 原文地址:https://www.cnblogs.com/wxdonly/p/5097181.html
Copyright © 2011-2022 走看看