【问题标题】:Redraw view inside of self.view on viewDidLoad() in swift?快速在 viewDidLoad() 上的 self.view 中重绘视图?
【发布时间】:2015-12-07 23:41:04
【问题描述】:

我正在开发一个应用程序,该应用程序的视图根据其屏幕尺寸定位在不同的位置。大小取决于设备。我选择了 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)?

【问题讨论】:

  • DeviceWidth 替换为UIScreen.mainScreen().bounds.widthDeviceHeight 替换为UIScreen.mainScreen().bounds.height 怎么样?

标签: swift uiview swift2 swift2.1


【解决方案1】:

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

例如:

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

为了清楚起见,我引用了self.view 而不仅仅是viewUIViewController 具有 view 属性。这可用于您尝试引用设备大小的地方。

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

此外,我建议您查看 Auto Layout 来解决这个特殊问题。它旨在帮助支持各种尺寸屏幕上的用户界面。以下是有关它的更多信息的链接。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2012-04-17
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多