IOS复习-UIButton

今天配置cnblogs总是出现一点问题,由于没有太多时间,最近一直忙于找工作,就没有花时间研究,但是metaweblog api就是有问题

切入正题,首先新建一个Single View Application模板的项目,如下图



再输入项目信息,如下图



接着打开chenViewController.m

再viewDidLoad方法添加代码,如下

- (void)viewDidLoad

{

    [superviewDidLoad];

//新建实例

    UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    //按钮大小,100100是以父视图左上角为原点的坐标,9050分别是长度和宽度

    CGRect frame=CGRectMake(1001009050);

    btn.frame=frame;

    //设置按钮上的文字

    [btn setTitle:@"我是按钮" forState:UIControlStateNormal];

    //设置按钮的背景色

    [btn setBackgroundColor:[UIColorredColor]];

    //设置按钮文字的背景色

    [btn.titleLabelsetBackgroundColor:[UIColorblueColor]];

    //添加单击事件,@selector(buttonClicked)是调用buttonClicked方法,事件是UIControlEventTouchUpInside

    [btn addTarget:nilaction:@selector(buttonClicked)forControlEvents:UIControlEventTouchUpInside];

    //添加到视图

    [self.view addSubview:btn];

}


//UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置按钮类型,按钮类型定义在一个枚举类型中
//typedef enum {
//    UIButtonTypeCustom = 0, // 没有风格
//    UIButtonTypeRoundedRect,// 圆角风格按钮
//    UIButtonTypeDetailDisclosure,//
//    UIButtonTypeInfoLight,// 明亮背景的信息按钮
//    UIButtonTypeInfoDark,// 黑暗背景的信息按钮
//    UIButtonTypeContactAdd,//
//} UIButtonType;
//但是考虑的ios开发中,为了界面美观一般设置背景图片,代替按钮的标题设置
//在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:
//UIControlEventTouchDown      // 按下
//UIControlEventTouchDownRepeat  // 多次按下
//UIControlEventTouchDragInside   // 保持按下然后在按钮及其一定的外围拖动
//UIControlEventTouchDragOutside  // 保持按下,在按钮外面拖动
//UIControlEventTouchDragEnter  // DragOutside进入DragInside触发
//UIControlEventTouchDragExit  // in到out触发
//UIControlEventTouchUpInside // 在按钮及其一定外围内松开
//UIControlEventTouchUpOutside // 按钮外面松开
//UIControlEventTouchCancel   // 点击取消


#pragma mark -事件
#pragma mark 单击事件
-(void)buttonClicked
{
    NSLog(@"按钮被点击");
}
添加了这个点击事件,控制台会输出按钮被点击,如下图



今天暂时写到这里,虽然只复习了一下UIButton,但是花费事件很长,全部用心设想了代码和图片的布局界面,怎样再博客上完美。

                                2013年8月7日,9:33,东南大学无锡分校桃园3宿舍106室

 

 

下面是补充的。

[myButon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置Button没有被选择的时候的字体颜色,选中后就变成默认的蓝色字体
    myButon.titleLabel.textColor = [UIColor blackColor];//也是字体颜色设置,和上没方法一样
    [myButon setBackgroundColor:[UIColor clearColor]];//背景色设置
//    myButon.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//button标题对齐方式,默认居中
//    给myButton添加一个事件buttonClicked
    [myButon addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

原本是离线编辑器做的,但是配置一直出现问题,提示html而非xml。索性不设置了,效果不是非常好,离线博客如图

原文地址:https://www.cnblogs.com/ioschen/p/3248808.html