IOS 在storyboard中创建按钮,用代码控制按钮

来源:互联网 发布:网络布线从入门到精通 编辑:程序博客网 时间:2024/05/14 12:01

1.在工程中找到Main.storyboard文件。


2.在Xcode右下角点击一个圆形的table标签,并找到Button控件。


3.点击Xcode右上角,有个双圆环的按钮。


4.按住ctrl建,并用鼠标左键点击按钮,拖向ViewController.h文件中,放开手并输入相应的名称。




这个时候在ViewController中就会多出来一个,你自己建的按钮信息,并和Main.storyboard中的按钮关联。


5.我们就可以操作这个按钮。我在ViewController.m中的viewDidLoad中,可以改变按钮不同状态下显示的内容,

在不同事件下的,操作。设置按钮的背景色,按钮上的文字颜色,按钮上的字体大小,

置按钮变成圆角,设置按钮边框的大小,这个设置按钮边框的颜色,设置阴影的颜色,

设置阴影的大小,设置阴影的透明度,等

代码如下:


////  ViewController.m//  TestUI1////  Created by Lioncraft on 5/9/15.//  Copyright (c) 2015 Lioncraft. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    [self.MyTestBtn setTitle:@"可以松手~" forState:UIControlStateHighlighted];//对于点击下,改变按钮的文字内容    //forState总共有6种状态//    UIControlStateNormal       = 0,//    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set//    UIControlStateDisabled     = 1 << 1,//    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)//    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use//    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use            [self.MyTestBtn addTarget:self action:@selector(myTestBtn:) forControlEvents:UIControlEventTouchUpInside];    /**     *action:@selector 后面是要调用的函数          forControlEvents总共有19种事件     分别是:     UIControlEventTouchDown           = 1 <<  0,      // on all touch downs     UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)     UIControlEventTouchDragInside     = 1 <<  2,     UIControlEventTouchDragOutside    = 1 <<  3,     UIControlEventTouchDragEnter      = 1 <<  4,     UIControlEventTouchDragExit       = 1 <<  5,     UIControlEventTouchUpInside       = 1 <<  6,     UIControlEventTouchUpOutside      = 1 <<  7,     UIControlEventTouchCancel         = 1 <<  8,          UIControlEventValueChanged        = 1 << 12,     // sliders, etc.          UIControlEventEditingDidBegin     = 1 << 16,     // UITextField     UIControlEventEditingChanged      = 1 << 17,     UIControlEventEditingDidEnd       = 1 << 18,     UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing          UIControlEventAllTouchEvents      = 0x00000FFF,  // for touch events     UIControlEventAllEditingEvents    = 0x000F0000,  // for UITextField     UIControlEventApplicationReserved = 0x0F000000,  // range available for application use     UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use     UIControlEventAllEvents           = 0xFFFFFFFF          **/        //这个是设置按钮的背景色    self.MyTestBtn.backgroundColor = [UIColor colorWithRed:1.00f green:0.0f blue:0.82f alpha:1.00f];        //这个是设置按钮上文字的颜色    [self.MyTestBtn setTitleColor:[UIColor colorWithRed:0.00f green:1.0f blue:0.02f alpha:1.00f] forState:UIControlStateNormal];    //这个是设置按钮上文字的大小    self.MyTestBtn.font = [UIFont boldSystemFontOfSize:12.0f];        //这个是设置按钮变成圆角    self.MyTestBtn.layer.cornerRadius = 10;    //这个是设置按钮边框的大小    self.MyTestBtn.layer.borderWidth = 3;    //这个设置按钮边框的颜色    self.MyTestBtn.layer.borderColor = [UIColor blackColor].CGColor;    //这个是设置阴影的颜色    self.MyTestBtn.layer.shadowColor = [UIColor yellowColor].CGColor;    //这个是设置阴影的大小    self.MyTestBtn.layer.shadowOffset = CGSizeMake(5, 2);    //这个是设置阴影的透明度    self.MyTestBtn.layer.shadowOpacity = 0.6;            CGRect btnFrame = self.MyTestBtn.frame;    //btnFrame.origin.y += 100;    btnFrame.size.width = 100;    btnFrame.size.height = 100;    self.MyTestBtn.frame = btnFrame;                   }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}//这个是弹出对话框的函数-(void)myTestBtn:(UIButton *)sender{        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"按钮点击提示" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];        [myAlertView show];    }@end

6.最后,改变按钮的大小和位置还有问题。

storyboard中设置了ui的大小和位置,在代码中修改ui大小和位置无响应。 

在storyboard中设置了一个label的大小和位置,想在代码中重新设定他的初始位置和大小,无响应。查找后发现storyboard在viewdidload之后才会加载,所以代码中的frame设置会被重置。这里是IOS新手,求大神解答如何解决这个问题(除了重新用代码编写。。。OTZ)。



0 0
原创粉丝点击