• 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即可。

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

  • 相关阅读:
    Trapping Rain Water
    Construct Binary Tree from Preorder and Inorder Traversal
    Flatten Binary Tree to Linked List
    Permutations II
    Unique Paths II
    Path Sum II
    Unique Binary Search Trees II
    evdev module-----uinput.py
    evdev module-----events.py
    evdev module-----device.py
  • 原文地址:https://www.cnblogs.com/xubaoaichiyu/p/5321477.html
Copyright © 2020-2023  润新知