zoukankan      html  css  js  c++  java
  • iOS开发——UI基础-按钮内边距,图片拉伸

    一、内边距

    UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距

    1、contentEdgeInsets

      如果是设置contentEdgeInsets, 会把UIImageView和UIlabel当做一个整体移动

    btn.contentEdgeInsets = UIEdgeInsetsMake(30, 0, 0, 0);

    对应状态:

    2、titleEdgeInsets/imageEdgeInsets

      如果是设置titleEdgeInsets/imageEdgeInsets. 那么不会影响到另外一个, 也就是只会改变当前设置的这个控件

        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    
        btn.imageEdgeInsets = UIEdgeInsetsMake(30 ,0 , 0, 0);

    二、图片拉伸

    1.iOS5以前

    UIButton *btn = [[UIButton alloc] init];
    
        UIImage *image = [UIImage imageNamed:@"common_button_blue_highlighted"];
    
    
    
        // LeftCapWidth: 左边多少不能拉伸
    
        // 右边多少不能拉伸 = 控件的宽度 - 左边多少不能拉伸 - 1
    
        //  right  =  width - leftCapWidth - 1
    
        // 1 = width - leftCapWidth - right
    
     
    
        // topCapHeight: 顶部多少不能拉伸
    
        // 底部有多少不能拉伸 = 控件的高度 - 顶部多少不能拉伸 - 1
    
        //  bottom =  height - topCapWidth - 1
    
        // 1 = height - topCapWidth - bottom
    
        UIImage *newImage = [image stretchableImageWithLeftCapWidth:5 topCapHeight:5];

    2.iOS5开始

        // UIEdgeInsets是告诉系统哪些地方需要受保护, 也就是不可以拉伸
    
        // resizableImageWithCapInsets默认拉伸方式是平铺
    
        UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.5, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5);
    
        UIImage *newImage =  [image resizableImageWithCapInsets:insets];

    3.iOS6开始

      // resizingMode指定拉伸模式
    
        // 平铺
    
        // 拉伸
    
        UIEdgeInsets insets = UIEdgeInsetsMake(5, 5, 5, 5);
    
        UIImage *newImage =  [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    
     
    
        [btn setBackgroundImage:newImage forState:UIControlStateNormal];
    
        btn.frame = CGRectMake(100, 100, 200, 80);
    
        [self.view addSubview:btn];
    将来的你会感谢今天如此努力的你! 版权声明:本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    TCP和UDP的一些区别: TCP提供可靠传输的机制:
    rpc和 http的区别
    熔断原理与实现Golang版
    源码解读 Golang 的 sync.Map 实现原理
    mysql底层为啥用b 树不用红黑树_MySQL索引底层数据结构
    一条sql 查询语句是如何执行的
    网络相关知识
    为什么遍历 Go map 是无序的?
    Go语言 参数传递究竟是值传递还是引用传递的问题分析
    解决goland debug 调试问题 Version of Delve is too old for this version of Go
  • 原文地址:https://www.cnblogs.com/chglog/p/4655888.html
Copyright © 2011-2022 走看看