简体   繁体   English

在HTTP请求后使用自动布局获取UIView框架

[英]Get UIView frame using autolayout after http request

I still don't understand how I should get the updated frame of a UIView , when using autolayout. 我仍然不明白在使用自动布局时如何获取UIView的更新框架。

Let's say I: 假设我:

  1. Setup some views using autolayout. 使用自动版式设置一些视图。
  2. Make an HTTP request to get some data. 发出HTTP请求以获取一些数据。
  3. When I get the data, I want to draw it but I need the updated frame size of a view. 当我获取数据时,我想绘制它,但是我需要更新的视图框架尺寸。

How should I go about it? 我应该怎么做?

My guess, but it's wrong: 我的猜测,但这是错误的:

layoutMyViews()
makeHttpRequest(callback)

func callback(data: MyData) {
    drawData(data, into:dataView)
}

func drawData(data: MyData, into view:UIView) {
    let size = view.frame.size
    // Here I want to draw data into view, depending on size, but it may be (0,0)
}

Here's just an example UIViewController - you can start your call in view did load and block the ui or at least indicate activity and then when that call completes you can update the UI based off of whatever current view properties you'd like. 这只是一个示例UIViewController-您可以在视图加载并阻止ui或至少指示活动时启动视图调用,然后在该调用完成时可以根据所需的任何当前视图属性来更新UI。

class ViewController: UIViewController {

// You can start accessing view properties from the storyboard here.
override func viewDidLoad() {
    super.viewDidLoad()
    print(self.view.frame) // Outputs the frame of the view controller's view.
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewDidLayoutSubviews() {
}

override func viewWillAppear(animated: Bool) {
}

override func viewDidAppear(animated: Bool) {
}
}

This is more or less what I'm doing right now. 这或多或少是我现在正在做的事情。 If anyone can suggest a better alternative please post or comment about it. 如果有人可以提出更好的选择,请发表评论。

As you will see, in drawDataIfPossible() I check that both the data is set and the frame size is set. 正如您将看到的那样,在drawDataIfPossible()我检查了是否设置了data并设置了帧大小。 I check both because drawDataIfPossible() is called from these two functions: 我都检查了一下,因为drawDataIfPossible()两个函数调用了drawDataIfPossible()

  1. drawData(data: MyData) , which might be called before the frame size is set drawData(data: MyData) ,可以在设置帧大小之前调用
  2. layoutSubviews() , which might be called before the data is set. layoutSubviews() ,可以在设置data之前调用它。

.

class Controller : UIViewController {

    // Here I will draw some data that comes from server
    var dataView: MyDataView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Here I add the views using autolayout, including dataView
        layoutMyViews()

        // Here I make the http request to get data
        makeHttpRequest(callback)
    }

    /** When the data comes, I tell the dataView to draw itself */
    func callback(data: MyData) {
        dataView.drawData(data)
    }

    //...
}

class MyDataView : UIView {

    var data: MyData!

    /** Draws the data in the view */
    func drawData(data: MyData) {
        self.data = data
        drawDataIfPossible()
    }

    func drawDataIfPossible() {
        // Here I check I have the data and that the frame size is set
        if data != nil && frame.size.width > 0 {
            drawData()
        }
    }

    /** Draws data in the view (needs the frame size) */
    func drawData {
        // ...
    }

    override func layoutSubviews()
    {
        super.layoutSubviews()
        layoutIfNeeded() // seems to be necessary for the frame size to be set
        drawDataIfPossible()
    }
}

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

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