swift 中的 viewDidLoad() 重绘 self.view 内的视图?

Redraw view inside of self.view on viewDidLoad() in swift?

我正在开发一个应用程序,该应用程序的视图根据屏幕大小位于不同的位置。大小取决于设备。我选择了iPhone 5S的尺寸。我的应用程序将在 iPad 上 运行。 查看 Swift 代码如何:

class Main: UIViewController {

    @IBOutlet var viewRect: UIView!

    let DeviceHeigth:CGFloat = 1136 / 2
    let DeviceWith:CGFloat = 640 / 2

    override func viewDidLoad() {
        super.viewDidLoad()

        calcviewRectViewInit()

    }

    func calcviewRectViewInit(){

        let space:CGFloat = 40
        var frame: CGRect = viewRect.frame
        frame.size.height = DeviceHeigth
        frame.size.width = DeviceWith
        frame.origin = CGPoint(x: toolView.frame.width + space, y: space)

        viewRect.frame = frame
    }

启动时,Rect 视图未按代码定位在大小和位置中。 viewDidLoad 不会在 运行 时间重新绘制视图。

我的问题是:如何在启动应用程序时重绘视图(viewRect)?

您应该使用父视图的框架作为参考。

例如:

var frame: CGRect = viewRect.frame
frame.size.height = self.view.frame.height / 2
frame.size.width = self.view.frame.width / 2

为了清楚起见,我引用了 self.view 而不是 viewUIViewControllerview 属性。这可以在您尝试引用设备尺寸的地方使用。

在构建此框架之前,您还应确保已为 toolView 正确设置框架。

此外,我建议查看自动布局以解决这个特定问题。它旨在帮助支持各种尺寸屏幕上的用户界面。下面是 link 以获取有关它的更多信息。

https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/index.html