Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在swift playgounds liveview中添加子视图以测试约束,但未能显示,添加的内部视图已消失_Swift_Xcode - Fatal编程技术网

我想在swift playgounds liveview中添加子视图以测试约束,但未能显示,添加的内部视图已消失

我想在swift playgounds liveview中添加子视图以测试约束,但未能显示,添加的内部视图已消失,swift,xcode,Swift,Xcode,我想在swift playgounds liveview中添加子视图以测试约束,但未能显示,添加的内部视图已消失。它只是在快速查看中绘制,但在liveview中消失了,如何使用constaints修改内部视图的位置 //: Playground - noun: a place where people can play import UIKit //import KeyBoardWubi import PlaygroundSupport var str = "Hello, playgroun

我想在swift playgounds liveview中添加子视图以测试约束,但未能显示,添加的内部视图已消失。它只是在快速查看中绘制,但在liveview中消失了,如何使用constaints修改内部视图的位置

//: Playground - noun: a place where people can play

import UIKit
//import KeyBoardWubi
import PlaygroundSupport

var str = "Hello, playground"

var u1 : UIView

//u1 = KeyBoardWubi3(frame: CGRect(x: 20, y: 20, width: 200, height: 200))
//u1

var v1,v2,v3 : UIView

func addview(vin:UIView){
    var v4 : UIView
    v4 = UIView(frame : CGRect(x: 40, y: 40, width: 800, height: 100))
    v4.backgroundColor = UIColor.blue
    vin.addSubview(v4)
    v4.translatesAutoresizingMaskIntoConstraints = false
    v4.leftAnchor.constraint(equalTo: vin.leftAnchor, constant: 12.0).isActive = true
}

v1 = UIView(frame : CGRect(x: 0, y: 20, width: 200, height: 200))
v1.backgroundColor = UIColor.red
PlaygroundPage.current.liveView = v1
addview(vin: v1)

v1

您的自动布局约束无效。它只有左锚。游乐场不会就无效约束向您发出警告。如果这是一个真正的应用程序,你早就知道了

试试这个:

import UIKit
import PlaygroundSupport

func addview(vin:UIView){
    // Since you are using auto-layout, the initial frame is irrelevant
    let v4 = UIView(frame: .zero)
    v4.backgroundColor = .blue

    // Set this **before** adding v4 to another view
    v4.translatesAutoresizingMaskIntoConstraints = false

    vin.addSubview(v4)

    // Playground doesn't warn you about invalid auto layout constraints
    // This is where you set the size and position of the new view
    v4.leftAnchor.constraint(equalTo: vin.leftAnchor, constant: 40).isActive = true
    v4.topAnchor.constraint(equalTo: vin.topAnchor, constant: 40).isActive = true
    v4.widthAnchor.constraint(equalToConstant: 800).isActive = true
    v4.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

let v1 = UIView(frame : CGRect(x: 0, y: 20, width: 200, height: 200))
v1.backgroundColor = UIColor.red
addview(vin: v1)
PlaygroundPage.current.liveView = v1

删除行
v4.leftAnchor.constraint(equalTo:…
似乎有效…是的,但是v4视图位置将设置为默认值,我只想修改v4在v1中的位置。我该如何做?