• IOS 杂笔-11(实现在外部无法改变UIView的size)


    我想题目说的或许不是很清楚,那么现在我详细介绍一下这篇随笔内容。

    在外部无法改变UIVIew控件的size。

    这里说是UIView,但是事实上,是大多数控件而绝非仅UIView。

    想要实现在外部无法改变size该怎么做呢。

    首先是重写setFrame使其规定本身size,如下

    复制代码
    //
    //  TestView.m
    //  CX-实现在外部无法改变UIView的Size
    //
    //  Created by ma c on 16/3/25.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "TestView.h"
    
    @implementation TestView
    
    -(void)setFrame:(CGRect)frame{
        
        frame.size = CGSizeMake(100, 100);
        
        [super setFrame:frame];
    }
    
    @end
    复制代码

    重写setFrame后我们可以进行测试。

    在VC里我吧TestVIew的size 设置为{200,200}。

    由此可见,在外部无法改变UITestView的Size

    但是下面的结果却并非如此

    我们先是设置UITestView的Center。

    然后设置UITestView的Bounds

    复制代码
    //
    //  ViewController.m
    //  CX-实现在外部无法改变UIView的Size
    //
    //  Created by ma c on 16/3/25.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "TestView.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        
        TestView * view = [[TestView alloc]init];
        
        view.center = self.view.center;
        
        view.bounds = CGRectMake(0, 0, 200, 200);
        
        [self.view addSubview:view];
        
        NSLog(@"%@",NSStringFromCGRect(view.frame));
        
    }
    
    
    
    @end
    复制代码

    结果如下

    可见:UITestView 的size有所改变,没关系。

    我们再重写一下bounds。

    复制代码
    //
    //  TestView.m
    //  CX-实现在外部无法改变UIView的Size
    //
    //  Created by ma c on 16/3/25.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "TestView.h"
    
    @implementation TestView
    
    -(void)setFrame:(CGRect)frame{
        
        frame.size = CGSizeMake(100, 100);
        
        [super setFrame:frame];
    }
    -(void)setBounds:(CGRect)bounds{
        
        bounds.size = CGSizeMake(100, 100);
        
        [super setBounds:bounds];
    }
    @end
    复制代码

    结果如下:

    复制代码
    //
    //  TestView.m
    //  CX-实现在外部无法改变UIView的Size
    //
    //  Created by ma c on 16/3/25.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "TestView.h"
    
    @implementation TestView
    
    -(void)setFrame:(CGRect)frame{
        
        frame.size = CGSizeMake(100, 100);
        
        [super setFrame:frame];
    }
    -(void)setBounds:(CGRect)bounds{
        
        bounds.size = CGSizeMake(100, 100);
        
        [super setBounds:bounds];
    }
    @end
    复制代码

    由此得出结论,如果想要是UIView控件在外部无法改变size,我们只需要重写frame,bounds即可。

    同理,我们还可以实现一些其他的操作。

  • 相关阅读:
    11月12号实验课
    10月29日实验
    10.15实验课作业
    Idea的使用和设置
    常用的Linux命令
    KAFKA总结
    SpringBoot+Maven+MyBaties+Mysql快速搭建一个项目
    Spring 常见面试
    Windows下安装ZK kafka Mysql dubbo redis
    MySQL常见的面试题
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045597.html
Copyright © 2020-2023  润新知