zoukankan      html  css  js  c++  java
  • 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即可。

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

  • 相关阅读:
    Centos7 下安装python3及卸载
    centos python3 的 卸载 删除
    Centos7安装python3和pip3(一)
    python升级带来的yum异常(解决错误File "/usr/bin/yum", line 30 except KeyboardInterrupt, e:)
    python 错误信息是:sudo :apt-get:command not found
    优化页面访问速度(四) ——前端优化
    优化页面访问速度(三) ——服务端优化
    优化页面访问速度(二) ——数据库优化
    优化页面访问速度(一)——综述
    Redis专题——Redis管理工具
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045597.html
Copyright © 2011-2022 走看看