在http请求之后使用autolayout获取UIView帧

时间:2016-03-16 01:07:22

标签: ios swift layout autolayout

使用自动布局时,我仍然不明白如何获取=IF(ISNUMBER(SEARCH("ONE",(IFERROR(VLOOKUP($E3,'Membership'!$A$2:$S$6500,19,FALSE),"N")))), "ONE", "N") =IF(ISNUMBER(SEARCH("TWO",(IFERROR(VLOOKUP($E3,'Membership'!$A$2:$S$6500,19,FALSE),"N")))), "TWO", "N") 的更新帧。

我想说:

  1. 使用autolayout设置一些视图。
  2. 发出HTTP请求以获取一些数据。
  3. 当我获取数据时,我想绘制它,但我需要更新的视图帧大小。
  4. 我应该怎么做?

    我的猜测,但这是错的:

    UIView

2 个答案:

答案 0 :(得分:0)

这里只是一个示例UIViewController - 您可以在视图中启动您的调用加载和阻止ui或至少指示活动,然后当该调用完成时,您可以根据您当前的任何视图属性更新UI #39;喜欢。

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) {
}
}

答案 1 :(得分:0)

这或多或少是我现在正在做的事情。如果有人可以提出更好的选择,请发布或评论。

正如您将看到的,在drawDataIfPossible()中,我检查是否设置了data并设置了帧大小。我检查两者是因为从这两个函数中调用了drawDataIfPossible()

  1. drawData(data: MyData),可能在设置帧大小之前调用
  2. layoutSubviews(),可能会在data设置之前调用。
  3. 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()
        }
    }