iOS学习 用代码实现界面

来源:互联网 发布:mac版本炒股软件 编辑:程序博客网 时间:2024/06/05 05:14

实现界面布局的代码一般放在 viewDidLoad 方法中,如下:

// 加载完成被调用- (void)viewDidLoad{     // 千万不要忘记调用父类的实现方法    [super viewDidLoad];    /**    使用 alloc init 方法实例化的按钮,就是 custom 类型,按钮的类型一旦指定,不能修改    如果创建其他类型的按钮    */    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeContactAdd];    btn1.center = CGPointMake(20,40);    [self.view addSubview:btn1]        UIButton *btn [[UIButton alloc] initWithFrame:CGRectMake(110,300,100,100)];    // 指定到 btn, 分布内存空间    self.iconButton = btn;        // 设置背景颜色    btn.backgroundColor = [UIColor redColor];    // 设置背景图片    [btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];    [btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighLighted];    // 设置按钮文字    [btn setTitle:@"点我" forState:UIControlStateNormal];    [btn setTitle:"你有病啊" forState:UIControlHighLighted];    // 设置文字颜色    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighLighted];    // 文字垂直对齐方式    btn.contentVerticalAligment = UIControlContentVerticalAligmentButton;        // 将按钮添加到视图    [self.view addSubview:btn];}


0 0
原创粉丝点击