简体   繁体   English

以编程方式添加某些内容时,UIView发生了变化-Swift

[英]UIView changed when something is added programmatically - Swift

I am working on an app for iPad. 我正在为iPad开发应用程序。 In this app, I use a Container View Controller and a View Controller. 在这个应用程序中,我使用了容器视图控制器和视图控制器。 Before the question, let me show you how it works: 在提问之前,让我向您展示其工作原理:

在此处输入图片说明

And my Swift code: 而我的Swift代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var containerView: UIView!

    @IBOutlet var blueView: UIView!

    var frameSize: CGSize!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(animated: Bool) {
        frameSize = self.view.frame.size
        containerView.frame.origin = CGPoint(x: frameSize.width - containerView.frame.width, y: 0)

        let deviceSize = CGSize(width: 375, height: 667)

        blueView.frame = CGRect(origin: CGPoint(x: frameSize.width / 2 - deviceSize.width / 2 - containerView.frame.width / 2, y: frameSize.height / 2 - deviceSize.height / 2), size: deviceSize)      

    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        let button = UIButton(frame: CGRect(origin: CGPoint(x: 20, y: 20), size: CGSize(width: 100, height: 50)))
        button.titleLabel?.text = "Button Test"
        button.backgroundColor = .redColor()

        blueView.addSubview(button)

    }

}

I run it, and everything occurs as expected. 我运行它,一切按预期进行。

在此处输入图片说明

The problem is, when I touch the screen, running code in touchesBegan. 问题是,当我触摸屏幕时,在touchesBegan中运行代码。

The problem is add programativamente anything. 问题是要添加programativamente。 I can't fix it. 我无法解决。 And my screen looks like this: 我的屏幕看起来像这样:

在此处输入图片说明

I can't identify what the origin and size change when I add something programmatically. 以编程方式添加内容时,我无法确定原点和大小会发生什么变化。

Can someone help me? 有人能帮我吗?

This occurs because the option ' Use Auto Layout '. 发生这种情况是因为选项“ 使用自动版式 ”。 Let disabled to solve the problem. 残疾人来解决问题。

在此处输入图片说明

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

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