简体   繁体   English

设备大小快速变化时,如何获取子视图的当前大小?

[英]how to get current size of a subview when device size changes in swift?

I have to view: view_1 and view_2 . 我必须查看:view_1和view_2。 view_1 height is proportional to the main view controller height and I am adding view_2 programmatically as a subview of view_1 with a proportional height. view_1的高度与主视图控制器的高度成正比,我以编程方式将view_2作为view_1的子视图添加成比例的高度。

Problem 问题

when the device size changes say from iPhone 6 to iPhone 5, the view_1 adjust correctly but its height I am passing to a function to draw its subview view_2 is still what was set in interface builder and so it is out of bounds in iPhone 4 moving from iPhone 6 当设备尺寸从iPhone 6更改为iPhone 5时,view_1可以正确调整,但我传递给其绘制子视图的功能的高度view_2仍然是在界面生成器中设置的,因此在iPhone 4移动中超出范围从iPhone 6

Thus I want to know how can I get the correct size of a view that what resize automatically to fit the current device? 因此,我想知道如何获取正确的视图大小,然后自动调整大小以适合当前设备?

The size of the view isn't established until Auto Layout runs. 自动版式运行之前,不会确定视图的大小。 Your first opportunity to get the correct size is in viewDidLayoutSubviews : 获得正确大小的第一个机会是在viewDidLayoutSubviews

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // access view size here
}

Here's a complete example that adds a second view as a subview of the first view. 这是一个完整的示例,其中添加了第二个视图作为第一个视图的子视图。 The red view was added in the Storyboard; 红色视图已添加到情节提要中。 it is centered horizontally and vertically and it's height/width is 50% of its superview. 它在水平和垂直方向上居中,其高度/宽度是其总视图的50%。

The yellow view is added programmatically. 黄色视图以编程方式添加。 It's frame is computed from the frame of the red view to be 50% of the width of the red view. 它的框架是根据红色视图的框架计算得出的,是红色视图宽度的50%。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var view1: UIView!
    var view2: UIView?

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if view2 == nil {
            view2 = UIView()
            view2?.backgroundColor = .yellowColor()
            view1.addSubview(view2!)
        }

        let width = view1.frame.size.width
        let height = view1.frame.size.height

        let frame = CGRect(x: width * 0.25, y: height * 0.25, width: width * 0.5, height: height * 0.5)

        view2?.frame = frame
    }
}

Not that viewDidLayoutSubviews gets called multiple times, so you must take care not to add view2 every time it is called. 不是说viewDidLayoutSubviews会被多次调用,所以您必须注意不要在每次调用view2都添加它。 That is why the code checks to see if view2 has been added. 这就是为什么代码检查是否已添加view2


iPhone 4s模拟器肖像


iPhone 5s模拟器风景


iPhone 5s旋转示例

Try this,you can find the screen size of device. 试试看,您可以找到设备的屏幕尺寸。

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

Try to do this in Viewdidload First you need to set view one frame 尝试在Viewdidload中执行此操作首先,您需要将视图设置为一帧

view_1.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height) 

then set frame of view_2 然后设置view_2的框架

view_2.frame = CGRectMake(0 , 0, view_1.frame.width, view_1.frame.height) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM