代码控制UI界面

来源:互联网 发布:淘宝有哪些好玩的东西 编辑:程序博客网 时间:2024/04/25 11:34

代码控制UI界面简介:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 创建UIWindow对象,并将该UIWindow初始化为与屏幕相同大小    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // 设置UIWindow的背景色    self.window.backgroundColor = [UIColor whiteColor];    // 创建一个UIViewController对象    UIViewController* controller = [[UIViewController alloc] init];    // 让该程序的窗口加载、并显示viewController视图控制器关联的用户界面    self.window.rootViewController = controller;    // 创建一个UIView对象    UIView* rootView = [[UIView alloc] initWithFrame        :[[UIScreen mainScreen] bounds]];    // 设置controller显示rootView控件    controller.view = rootView;    // 创建一个圆角按钮    UIButton* button = [UIButton buttonWithType: UIButtonTypeRoundedRect];    // 设置按钮的大小    button.frame = CGRectMake(120, 100, 80, 40);    // 为按钮设置文本    [button setTitle:@"确定" forState:UIControlStateNormal];    // 将按钮添加到rootView控件中    [rootView addSubview:button];    // 创建一个UILabel对象    self.show = [[UILabel alloc] initWithFrame        :CGRectMake(60 , 40 , 180 , 30)];    // 将UILabel添加到rootView控件中    [rootView addSubview:self.show];    // 设置UILabel默认显示的文本    self.show.text = @"初始文本";    self.show.backgroundColor = [UIColor grayColor];    // 为圆角按钮的触碰事件绑定事件处理方法    [button addTarget:self action:@selector(clickHandler:)        forControlEvents:UIControlEventTouchUpInside];    // 将该UIWindow对象设为主窗口、并显示出来    [self.window makeKeyAndVisible];    return YES;}

自定义UI控件

定义一个集成View基类的子类,然后重写View类的一个或多个方法,通常重写的方法如下:

  • initWithFtame: 初始化用到,当需要额外初始化时,需重写该方法
  • initWithCoder: 程序通过在nib文件中加载完该控件后回自动调用该方法,因此如果程序需要在nib文件加载玩后执行自定义初始化,可重写该方法
  • drawRect: 程序需要自行绘制该控件内容时重写该方法
  • layoutSubviews: 需要对控件所包含的子控件布局进行更精密的控制时重写该方法
  • didAddSubview: 添加子控件完成时会激发该方法
  • willRemoveSubview: 将要删除子控件时会激发该方法
  • willMoveToSuperview: 该控件将要添加到父控件时会激发该方法
  • didMoveToSuperview: 该控件添加到父控件完成时会激发该方法
  • willMoveToWindow: 该控件将要添加到窗口时会激发该方法
  • didMoveToWindow: 该控件添加到窗口完成时会激发该方法
  • touchesBegan:withEvent: 用户手指开始触碰该控件时激发该方法
  • touchesMoved:withEvent: 用户手指在该控件上移动时激发该方法
  • touchesEnded:withEvent: 用户手指结束触碰该控件时激发该方法
  • touchesCancelled:withEvent: 用户取消触碰该控件时激发该方法
0 0
原创粉丝点击