欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ios bounds和frame的区别(bounds的应用)

程序员文章站 2022-05-30 12:02:27
...

苹果官方文档定义连接:
bounds-UIView | Apple Developer Documentation
返回上级目录:IOS文档学习

1.定义和比较

  • bounds:在自己的坐标系中描述view的位置和大小

  • frame: 在父视图的坐标系中描述view的位置和大小

  • 改变view.frame的x和y值会改变view在父视图中的位置,而改变view.bounds的x和y值会改变view的子视图在view中的位置

2.通过改变bounds的x和y来改变子视图的位置(demo(代码+gif))

2.1 如下图,绿图和蓝图是黄图的子视图,红图是绿图的子视图,初始和还原状态下所有视图的bounds的x和y值都是0,从图中我们可以观察和推导出:

  • view.bounds的x值增加,所有的子视图会向左跑
  • view.bounds的x值减少,所有的子视图会向右跑
  • view.bounds的y值增加,所有的子视图会向上跑
  • view.bounds的y值减少,所有的子视图会向下跑
    ios bounds和frame的区别(bounds的应用)
    2.2 上面示例的代码实现
//
//  viewController.swift
//  Bounds&Frame
//
//  Created by macvivi on 2020/5/23.
//  Copyright © 2020 macvivi. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var yellowView:UIView?
    var greenView:UIView?
    var redView:UIView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
         setupUI();
    }
    
    
    func setupUI(){
        
        yellowView = UIView.init(frame: CGRect(x: 0, y: 44, width: 350, height: 600));
        yellowView?.backgroundColor = UIColor.yellow;
        view.addSubview(yellowView ?? view);
        
        greenView = UIView.init(frame: CGRect(x: 100, y: 100, width: 200, height: 250))
        greenView?.backgroundColor = UIColor.green
        yellowView?.addSubview(greenView ?? view);
        
        redView = UIView.init(frame: CGRect(x: 50, y: 50, width: 100, height: 100));
        redView?.backgroundColor = UIColor.red
        greenView?.addSubview(redView ?? view)
        
        let blueView = UIView.init(frame: CGRect(x: 80, y: 400, width: 100, height: 100));
        blueView.backgroundColor = UIColor.blue;
        yellowView?.addSubview(blueView)
        
        
        let btn =  UIButton.init(type: .system)
        btn.frame = CGRect(x: 150, y: 400, width: 200, height: 30);
        btn.setTitle("绿图bounds的y加100", for: .normal);
        view.addSubview(btn);
        
        let btn1 = UIButton.init(type: .system);
        btn1.frame = CGRect(x: 150, y: 500, width: 200, height: 30);
        btn1.setTitle("还原", for: .normal);
        view.addSubview(btn1);
        
        let btn2 = UIButton.init(type: .system);
        btn2.frame = CGRect(x: 150, y: 600, width: 200, height: 30);
        btn2.setTitle("黄图bounds的x和y都加50", for: .normal);
        view.addSubview(btn2);
        
        
        btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside);
        btn1.addTarget(self, action: #selector(restore(btn:)), for: .touchUpInside);
        btn2.addTarget(self, action: #selector(allAdd), for: .touchUpInside)
    }
    
    
    @objc func restore(btn:UIButton){
        greenView?.bounds = CGRect(x: 0, y: 0, width: 200, height: 250);
        yellowView?.bounds = CGRect(x: 0, y: 0, width: 350, height: 600);
    }
    
    
    @objc func btnClick(btn:UIButton){
        greenView?.bounds = CGRect(x: 0, y: 100, width: 200, height: 250);
        
    }
    

    @objc func allAdd(){
        yellowView?.bounds = CGRect(x: 50, y: 50, width: 350, height: 600);
    }
    
}


相关标签: ios文档学习 ios