美文网首页
UI基础控件

UI基础控件

作者: 爱吃芒果的淼小猪 | 来源:发表于2016-02-21 12:07 被阅读0次

    //准备工作

    1.删除Main 2.ARC->MRC(YES->No) 3.删除文件(ViewConTroller.h/.m) 4.strong->retain(AppDelegate.H)5.重写dealloc

    //第一步:创建window和当前屏幕一样大[UIScreen mainScreen].bounds

    self.window = [UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //第二步:设置window颜色

    self.window.backgroungColor = [UIColor whiteColor];

    //第三步:设置window可见

    [self.window makeKeyAndVisible];

    //第四步:xcode7崩溃解决

    self.window.rootViewController = [UIViewController alloc]init];

    [UIViewController alloc]init];

    //第五步:内存管理

    [_window release];

    //1.创建试图 设置frame

    UIView *firstView = [UIView alloc]initWithFrame:CGRectMake(0,0,100,100)];

    //2.设置属性

    firstView.bavkgroundColor = [UIColor redColor];

    //3.添加到父试图(window)显示

    [self.window addSubview:firstView];

    //创建blueView

    UIView *blueView = [UIView alloc] initWithFrame:CGRectMake(0,0,200,200)];

    blueView.backgroundColor = [UIColor blueColor];

    [self.window addSubview:blueView];

    [blueView release];

    //中心显示

    //中心显示

    //数值设置(绝对坐标)

    blueView.center = CGPointMake(375/2,667/2);

    //通过其他控制件位置设置(相对坐标)

    blueView.center = self.window.center;

    //改变frame

    blueView.frame = CGRectMake(200,100,100,200);

    //居中

    self.window addSubView:blueView;

    //显示隐藏

    aView.hidden = NO;

    //透明度alpha(0-1的浮点数)

    aView.alpha = 1;

    //标记值:tag

    aView.tag = 1000;

    //改变颜色:通过标记寻找试图

    UIView*tempView = [self.window viewWithTag:1000];

    te,pView.backgroundColor = [UIColor purpleColor];

    //父视图(一个父视图只有一个父试图)

    //子视图(一个视图可以有若干个子视图)

    //随机颜色

    cView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

    //UILabel

    //1.创建+fram

       UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

       //2.属性设置

       label.backgroundColor = [UIColor yellowColor];

       //3.添加父视图

       [self.window addSubview:label];

       //4.内存管理

       [label release];

    /******label文字相关属性*****/

    //显示文字text

    //默认左对齐/居中显示/文本黑色/行数为1/背景透明色

    //居中改不了

    //文本颜色 textColor

    lable.textColor= [UIColor purpleColor];

    //文本对齐方式 textAlignment

    lable.textAlignment = NSTextAlignmentCenter;

    //断行模式(文本省略方式)lineBreakMode

    lable.lineBreakMode = NSLineBreakByTruncatingMiddle;//中间省略

    //文本行数 numberOflines

       //默认为1 为0时行数不限

       label.numberOfLines = 0;

       //label自适应 sizeToFit

       //如果文本不足一行宽高一起改变

       //如果文本超过一行 只改变高

       //一定要写在设置text之后 如果写在之前 label的size会置为0

       [label sizeToFit];

       //字体 font

       label.font = [UIFont systemFontOfSize:20];

       //阴影 shadow

       label.shadowColor = [UIColor blackColor];

       //阴影偏移量

       label.shadowOffset = CGSizeMake(10, 10);

    //键盘回收:

    //必须用到协议,设置代理人,实现方法;

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{

    //获取第一响应

    if(textField.tag ==设置的tag值){

    [textField resignFieldResponder];

    [[self.window viewWithTag:下一个tag值] becomeFirstResponder];}

    if(textField.tag ==下一个tag值){

    [textField resignFirstResponder];}

    return YES;

    //在输入框将要输入内容时 屏幕上移

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    if(textField.tag == tag值){

    [UIView animateWithDuration:设置时间 animation:^{

    self.window.center = CGPointMake(self.window.center.x,self.window.center.y - 258);

    }];

    }

    return YES;

    }

    //输入结束时 屏幕下移

    -(BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    if (textField.tag == 2000) {

    [UIView animateWithDuration:0.3 animations:^{

    self.window.center = CGPointMake(self.window.center.x, self.window.center.y + 258);

    }];

    }

    return YES;

    }

    //在两个按钮上设置tag值 点击时让它变成自己想要的颜色

    -(UIButton*)information:(UIButton*)sender {

    if (sender.tag == 1001) {

    NSLog(@"消息");

    sender.backgroundColor = [UIColor cyanColor];

    UIButton *tempButton = [self.view viewWithTag:1002];

    tempButton.backgroundColor = [UIColor whiteColor];

    } else if(sender.tag == 1002){

    NSLog(@"电话");

    sender.backgroundColor = [UIColor cyanColor];

    UIButton *tempButton = [self.view viewWithTag:1001];

    tempButton.backgroundColor = [UIColor whiteColor];

    }

    return sender;

    }

    UIControll控制类

    addTarget:action:forControllEvents:

    //添加响应事件(满足什么条件下 让某人调用方法)

    UISegmentedControl

    UISegmentedControl *seg=[[UISegmentedControl alloc] initWithtems:@[@"消息" ,@"电话",@"微信"]];

    seg.Frame = CGRectMake(100,100,200,40);

    [self.view addSubview:seg];

    [seg release];

    //选中分段下标

    seg.selectedSegmentIndex = 0;

    //一般不设置背景颜色  若要设置跟其他设置颜色的方法一样。

    //渲染颜色

    seg.tintColor = [UIColor lightGrayColor];

    //插入新的分段

    [seg insertSegmentWithTitle:@"陌陌" atIndex:2 animated:YES];

    //创建两个不同颜色的view

    self.redv = [[UIView alloc]initWithFrame:CGRectMake(50,200,300,300)];

    self.redv.backgrounderColor = [UIColor refColor];

    [self.view addSubview:self.redv];

    [_redv release];

    //相同的方法再建另一个颜色

    //分段控制器的方法调用

    -(void)segAction:(UISegmentedControl *)seg{if(seg.selectedSegmentIndex == 0)

    [UIView transitionFrameView:self.greenV toView:self.redv duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished){

    }];

    }

    if(seg.selectedSegmentIndex == 1){

    [UIView TransitionFromView:self.redv toView:self.greenV duration:1 options:UIViewAnimationOptionTransitionFileFromLeft completion:^(BOOL finished){

    }];

    //UISlider滑块控制器

    UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(50,50,250,50)];

    sl.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:sl];

    [sl release];

    //颜色设置:

    //划过距离的颜色(滑块左)

    sl.minimumTrackTintColor = [UIColor blackColor];

    //未划过的距离颜色(滑块右)

    sl.maxmumTrackTintColor =[UIColor redColor];

    //滑块颜色:

    sl.thumbTintColor =[UIColor greenColor];

    //添加响应事件:

    [sl.addTarget:self action:@selection(sliderAction:)forControlEvents:UIControlEventValueChanged];

    //滑动范围:

    //最小值:

    sl.minimumValue = -100;

    //最大值:

    sl.maximumValue = 1000;

    //更新滑块的起始点:

    sl.value = -100;

    //滑块控制器的方法调用:

    -(void)sliderAction:(UISlider *)sl{

    NSLog(@"%f",sl.value);

    }

    //页码控制器:

    UIPageControl *pc = [[UIPageControl alloc]initWithFrame:CGRectMake:(50,150,100,50)];

    pc.backgroundColor = [UIColor blackColor];

    [self.view addSubview:pc];

    [pc release];

    //页数:

    pc.numberOfPages = 4;

    //当前页:

    pc.currentPage = 3;

    //页码颜色:

    pc.pageIndicatorTintColor = [UIColor greenColor];

    //当前页颜色:

    pc.currentPageIndicatorTintColor = [UIColor redColor];

    //响应事件:

    [pc.addTarget:self action:@selection(pageAction:)forControlEvents:UIControlEventValueChanged];

    //页码控制器的方法调用:

    -(void)pageAction:(UIPageControl *)page{

    NSLog(@"%ld",page.currentPage);

    }

    //开关控制器:

    UISwitch *sw = [[UISwitch alloc]initWithFrame:CGRectMake(250,150,100,50)];

    sw.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:sw];

    [sw addTarget:self action:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];

    [sw release];

    //开关属性:

    sw.on = YES;

    sw.tintColor = [UIColor blackColor];

    sw.thumbTintColor = [UIColor redColor];

    sw.onTintColor = [UIColor purpleColor];

    //开关判断:

    -(void)switchAction:(UISwitch *)sw{

    if(sw.on){

    NSLog(@"开启");

    }else{

    NSLog(@"关闭");

    }

    }

    //创建vc

    RootViewController *rootVC = [[RootViewController alloc]init];

    //根视图

    self.window.rootViewController = rootVC;

    #pragma mark - 初始化

    -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

    NSLog(@"初始化");

    }

    return self;

    }

    #pragma mark - 加载视图

    -(void)loadView{

    //重写时 一定要写super

    //loadView方法 负责创建self.view

    [super loadView];

    NSLog(@"加载视图");

    }

    #pragma mark - 视图已经加载

    - (void)viewDidLoad {

    [super viewDidLoad];

    NSLog(@"视图已经加载");

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor lightGrayColor];

    //UI导航控制器

    //创建VC

    ViewController *root = [[ViewController alloc] init];

    //导航控制器: 管理控制器的控制器

    //创建导航

    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:root];

    //把导航设置为根视图

    self.window.rootViewController = navi;

    //导航栏设置:controller(栏)/item(栏上的元素)

    //导航栏显示/隐藏

    self.navigationController.navigationBarHidden = NO/YES;

    //栏样式

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    //半透明效果

    //开启效果时 屏幕左上角为坐标原点

    //关闭时 导航栏的左下角为坐标原点

    self.navigationController.navigationBar.translucent = YES;

    //栏背景颜色

    self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];

    //栏颜色

    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

    //栏标题

    //    self.title = @"这是一个标题 ";

    self.navigationItem.title = @"Back";

    //分段

    UISegmentedControl *seg = [[[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]] autorelease];

    seg.frame = CGRectMake(0, 0, 100, 30);

    //栏标题视图

    self.navigationItem.titleView = seg;

    //栏左侧按钮

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(left:)] autorelease];

    //栏右按钮

    //系统按钮样式

    UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];

    //自定义按钮图片

    UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gift.png"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];

    self.navigationItem.rightBarButtonItems = @[b1,b2];

    //修改导航栏上内容的颜色

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    //跳转页面

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame = CGRectMake(200, 200, 100, 100);

    btn.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:btn];

    [btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];

    #pragma mark - 跳转页面

    -(void)goTwo{

    //1.获取第二页面对象

    TwoViewController *twoVC = [[TwoViewController alloc] init];

    //2.跳转(由导航控制器 从当前push到第二页)

    [self.navigationController pushViewController:twoVC animated:YES];

    //3.内存管理

    [twoVC release];

    }

    //UI页面间传值

    #warning 属性1: 在第二页声明一个属性 用来保存数据

    @property(nonatomic,copy)NSString *string;

    #warning 属性2: 在push页面之前传值(创建对象之后 push之前)

    twoVC.string = self.tf1.text;

    #warning 属性3: 通过属性给当前页内容赋值

    self.tf2.text = self.string;

    #warning 协议1: 声明协议(定义一个带参数的方法)

    @protocol PassDelegate

    #warning 协议2: 定义代理人属性

    @property(nonatomic,assign)iddelegate;

    #warning 协议3: 返回上一页之前 让代理人调用协议方法

    [self.delegate passValue:self.tf2.text];

    [self.navigationController popViewControllerAnimated:YES];

    #warning 协议4: 签协议

    @interface RootViewController ()

    #warning 协议5: 设置代理人

    //为了保证设置代理人的对象和push的对象是一个 在创建push对象之前 设置delegate.

    twoVC.delegate = self;

    [self.navigationController pushViewController:twoVC animated:YES];

    [twoVC release];

    }

    #warning 协议6: 实现协议方法

    -(void)passValue:(NSString *)string{

    //把收到的string 赋值给输入框

    self.tf1.text = string;

    }

    //UIButton

    //1.常见属性

    1> titleLabel获取内部的UILabel对象

    2> imageView获取内部的UIImageView对象

    //2.常见方法

    // 设置内部UILabel显示的文本内容

    //设置按钮文本的时候不能  btn.titleLabel.text = @"4324324";

    - (void)setTitle:(NSString *)title forState:(UIControlState)state;

    //设置内部UILabel的文字颜色

    - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;

    // 设置内部UILabel的文字阴影颜色

    - (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;

    //设置内部UIImageView的图片

    //设置内部UIImageView的图片不能:btn.imageView.image = [UIImage imagedName:@"0.png"];

    - (void)setImage:(UIImage *)image forState:(UIControlState)state;

    // 设置背景图片

    - (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

    //下面两个方法需要交给子类去重写,然后用于重新布局button。

    //返回内部UILabel的frame(位置和尺寸)

    - (CGRect)titleRectForContentRect:(CGRect)contentRect;

    //返回内部UIImageView的frame(位置和尺寸)

    - (CGRect)imageRectForContentRect:(CGRect)contentRect;

    //下面这些方法可以获取不同状态下的一些属性值

    - (NSString *)titleForState:(UIControlState)state;

    - (UIColor *)titleColorForState:(UIControlState)state;

    - (UIColor *)titleShadowColorForState:(UIControlState)state;

    - (UIImage *)imageForState:(UIControlState)state;

    - (UIImage *)backgroundImageForState:(UIControlState)state;

    相关文章

      网友评论

          本文标题:UI基础控件

          本文链接:https://www.haomeiwen.com/subject/mvnvkttx.html