• UIButton


    1  ------ UIButton按钮是IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用。

      第一、UIButton的定义

      UIButton *button=[[UIButton buttonWithType:(UIButtonType);

      能够定义的button类型有以下6种,

      typedef enum {

      UIButtonTypeCustom = 0, 自定义风格

      UIButtonTypeRoundedRect, 圆角矩形

      UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

      UIButtonTypeInfoLight, 亮色感叹号

      UIButtonTypeInfoDark, 暗色感叹号

      UIButtonTypeContactAdd, 十字加号按钮

      } UIButtonType;

      第二、设置frame

      button1.frame = CGRectMake(20, 20, 280, 40);

      [button setFrame:CGRectMake(20,20,50,50)];

      第三、button背景色

      button1.backgroundColor = [UIColor clearColor];

      [button setBackgroundColor:[UIColor blueColor]];

      第四、state状态

      forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

      enum {

      UIControlStateNormal = 0, 常规状态显现

      UIControlStateHighlighted = 1 << 0, 高亮状态显现

      UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

      UIControlStateSelected = 1 << 2, 选中状态

      UIControlStateApplication = 0x00FF0000, 当应用程序标志时

      UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管

      };

      @property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently

      @property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application

      @property(nonatomic,getter=isHighlighted)BOOL highlighted;

      第五 、设置button填充图片和背景图片

      [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

      [buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

      第六、设置button标题和标题颜色

      [button1 setTitle:@"点击" forState:UIControlStateNormal];

      [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

      第七、设置按钮按下会发光

      button.showsTouchWhenHighlighted=NO;

      第八、添加或删除事件处理

      [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

      [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

      第九、 设置按钮内部图片间距和标题间距

      UIEdgeInsets insets; // 设置按钮内部图片间距

      insets.top = insets.bottom = insets.right = insets.left = 10;

      bt.contentEdgeInsets = insets;

      bt.titleEdgeInsets = insets; // 标题间距

    2  -------  两种不同的创建方式,其中一种button 可以显示titile,但是另外一种不能显示。如下所示;

    //UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    //使用这种创建方式,button 不能显示 titile
    //button.frame = CGRectMake(10+(Screen_width*schemeCount)+30+100, 20+(j*30), 130, 40); 
    //使用alloc的方式创建,button 可以显示title UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10+(Screen_width*schemeCount)+30+100, 20+(j*30), 130, 40)]; [button setTitle:@"1" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor whiteColor]]; [self.uisv_bottomView addSubview:button]; [button release];

     设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height);

    [btn setTitle: @"search" forState: UIControlStateNormal];

    //设置按钮上的自体的大小

    //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法

    //应该使用

    btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

    [btn seBackgroundColor: [UIColor blueColor]];

    //最后将按钮加入到指定视图superView

    [superView addSubview: btn];        //为什么要指定到superview?

    ==========================================================

    tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];

    这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,

    btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;//设置文字位置,现设为居左,默认的是居中

    但是问题又出来,此时文字会紧贴到做边框,我们可以设置

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

    使文字距离做边框保持10个像素的距离。

    =======================================================

    设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:

    [btn.titleLabel setTextColor:[UIColorblackColor]];

    btn.titleLabel.textColor=[UIColor redColor];

    而是用:

    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];

    长按按钮响应事件

    {

    //长按删除按钮
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(deleteLongPressed)];
    // longPress.minimumPressDuration = 0.5; //最短按压时间,一般地,此句不写也可以
    [uib_delete addGestureRecognizer:longPress];

    }

    //长按删除按钮,删除所有输入的号码
    -(void)deleteLongPressed
    {
    uil_phoneNumber.text = @"";
    nsms_phoneNumber = [[NSMutableString alloc]initWithFormat:@""];
    }

    UIButton 两种设置背景图片方式的区别

    第一种:setBackGroudImage   :图片被拉伸

    第二种:setImage     :  图片保持原大小

    非alloc 的button 都不要release ,否则button 显示不出来。虽然button的 retaincount 还是1 。如下

    //button 剧集
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, k_headImageView_height, SCREEN_WIDTH/2-1, 84/2-1);
    [button setBackgroundImage:[ImageUtilities createImageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [button setBackgroundImage:[ImageUtilities createImageWithColor:[ColorUtils colorWithHexString:k_buttonSelect_backGroudColor]] forState:UIControlStateSelected];
    [button setTitle:@"剧集" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:k_introduceInfo_fontSize]];
    [button setTitleColor:[ColorUtils colorWithHexString:k_introduceInfo_textColor] forState:UIControlStateNormal];
    [button setTitleColor:[ColorUtils colorWithHexString:k_buttonSelect_textColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(btnEpisodeClicked:) forControlEvents:UIControlEventTouchUpInside];
    episodeButton = button;
    [self.view addSubview:episodeButton];
    //[button release]; // 若此处添加 release, button 将显示不出来。

    让UIButton在按下时没有高亮效果

    方法一:

    button1.adjustsImageWhenHighlighted = NO;     //取消按钮高亮状态(有些时候,我们并不想要系统给的那个状态)

    方法二:为UIButton所有状态设置一张透明的图片。详情请看:让UIButton在按下时没有高亮效果设置按钮被点中的高亮光晕效果

    [cancelButton setShowsTouchWhenHighlighted:YES];

    设置UIButton 的字体阴影

    [[rightButton titleLabel] setShadowColor:[UIColor blackColor]];
    [[rightButton titleLabel] setShadowOffset:CGSizeMake(-0.5, -0.5)];

    调整Button内部内容的边距(Padding)
    [self.userNameButton setContentEdgeInsets:UIEdgeInsetsMake(0, 3, 0, 0)];

    UIButtonTypeCustom 和  UIButtonTypeRoundedRect  的区别

     UIButtonTypeCustom :是一个背景透明的按钮。  如果想做按钮中图片切换(不同状态),必须使用两张图片进行设置。

    UIButtonTypeRoundedRect :是一个圆角矩形按钮。方便之处:系统会自动为这种按钮设置高亮状态(半透明状态)。即,通过一张图片,就可以做出按钮交互的效果。 不方便的地方:如果你想自定义不同状态下,按钮显示的背景,这种类型的按钮就支持的不好。

    建议:快速开发,每个按钮只有一张图片,建议使用UIButtonTypeRoundedRect。按钮高度自定义,建议使用UIButtonTypeCustom。

    titleEdgeInsets  imageEdgeInsets 的使用

      [myButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)]; 4个参数是上边界,左边界,下边界,右边界。

    使用UIButton和UIImageView时的坑

    -》使用将UIButton设置为透明,然后使用UIImage 或者UIImageView作为UIButton的父view,可能会使UImage覆盖的那片UIButton区域失去事件响应。解决方案:设置UIimage或者UIImageView的人机交互属性为YES.     ***.userInteractionEnabled = YES;

    2、同理,如果将一个imageview作为uibutton的子view赋给uibutton,那么uibutton被覆盖的区域将失去事件响应能力。解决方案:让uibutton在上层,imageview在下层即可。注意,此处设置人机交互属性为YES也是没用的哦。 

    uibutton增加长按事件之后怎么识别自己uibutton.tag

    UIButton *btn =(UIButton*)gesture.view;NSLog(@"%d",btn.tag);

    
    
  • 相关阅读:
    Python小工具:统计代码行数
    计算机图形学复习(一)
    牛客多校训练第一场 J.Different Integers
    数据校验码概述
    数据库复习第二章
    数据库复习第一章
    自动化AC器(带界面版)
    ZOJ 3747 Attack on Titans
    Codeforces Round #245 (Div. 1) B. Working out
    HDU 6266 Hakase and Nano 【博弈论】
  • 原文地址:https://www.cnblogs.com/wxd3652/p/4938937.html
Copyright © 2020-2023  润新知