Ios 在Swift中以编程方式创建UITextView时未获得输出 AppDelegate.swift ViewController.swift UIViewUsingTextField.swift
通过使用上面的代码,我不会在模拟器中获得蓝色的UITextView。我想以编程方式创建UITextview,但我找不到实际问题,即为什么我不能将蓝色UITextview作为此代码的输出。我的模拟器中只有白色屏幕作为这段代码的输出。我已尝试按照链接编码。是否有人可以帮助我解决此问题以获得所需的输出。在视图中添加框架并禁用翻译自动调整大小GMaskintoConstraints将使其适合您Ios 在Swift中以编程方式创建UITextView时未获得输出 AppDelegate.swift ViewController.swift UIViewUsingTextField.swift,ios,iphone,swift,Ios,Iphone,Swift,通过使用上面的代码,我不会在模拟器中获得蓝色的UITextView。我想以编程方式创建UITextview,但我找不到实际问题,即为什么我不能将蓝色UITextview作为此代码的输出。我的模拟器中只有白色屏幕作为这段代码的输出。我已尝试按照链接编码。是否有人可以帮助我解决此问题以获得所需的输出。在视图中添加框架并禁用翻译自动调整大小GMaskintoConstraints将使其适合您 self.blueview?.frame = CGRectMake(0, 0, 200, 200) // s
self.blueview?.frame = CGRectMake(0, 0, 200, 200)
// self.blueview?.translatesAutoresizingMaskIntoConstraints = false
将自定义类设置为
视图
:
下面的代码将在视图中心添加200x200的blueView
:
如果您希望blueView覆盖整个视图:
是,我想为通用应用程序应用约束。如何将自定义类设置为视图?我可以获得蓝色框,但没有应用的约束。如果约束不正确,我想更正它们。我正试图根据此视频添加约束:“我如何才能更正错误,使其具有自动布局约束。我真的非常感谢您付出的宝贵努力。我正在尝试添加一个红色的UIView。我需要将每台设备水平和垂直放置在一起。这对具有自动布局功能的通用应用程序有效吗?
import UIKit
class ViewController: UIViewController {
override func loadView()
{
self.view = UIViewUsingTextField()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
class UIViewUsingTextField: UIView
{
var blueview: UIView?
init() {
super.init(frame: CGRectZero)
self.backgroundColor = UIColor.whiteColor()
self.setupView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
}
func setupView()
{
self.blueview = UIView()
self.blueview?.backgroundColor = UIColor.blueColor()
self.blueview?.translatesAutoresizingMaskIntoConstraints = false
addSubview(self.blueview!)
let centerXconstraint = NSLayoutConstraint(item: self.blueview!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
let centerYconstraint = NSLayoutConstraint(item: self.blueview!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
self.addConstraint(centerXconstraint)
self.addConstraint(centerYconstraint)
let widthConstraint = NSLayoutConstraint(item: self.blueview!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let heightConstraint = NSLayoutConstraint(item: self.blueview!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
self.addConstraint(widthConstraint)
self.addConstraint(heightConstraint)
self.addConstraints([centerXconstraint,centerYconstraint,widthConstraint,heightConstraint])
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
self.blueview?.frame = CGRectMake(0, 0, 200, 200)
// self.blueview?.translatesAutoresizingMaskIntoConstraints = false
import UIKit
class UIViewUsingTextField: UIView
{
var blueView : UIView?
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.whiteColor()
setupView()
}
func setupView() {
self.blueView = UIView()
self.blueView?.backgroundColor = UIColor.blueColor()
self.blueView?.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.blueView!)
let blueViewCenterXConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
let blueViewCenterYConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
let blueViewWidthConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
let blueViewHeightConstraint = NSLayoutConstraint(item: self.blueView!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 200)
self.addConstraints([blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint])
}
}
func setupView() {
self.blueView = UIView()
self.blueView?.backgroundColor = UIColor.blueColor()
self.blueView?.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(self.blueView!)
let constLeading = NSLayoutConstraint(item: self.blueView!,
attribute: NSLayoutAttribute.Leading,
relatedBy: .Equal,
toItem: self,
attribute: NSLayoutAttribute.Leading,
multiplier: 1,
constant: 0)
self.addConstraint(constLeading)
let constTrailing = NSLayoutConstraint(item: self.blueView!,
attribute: NSLayoutAttribute.Trailing,
relatedBy: .Equal,
toItem: self,
attribute: NSLayoutAttribute.Trailing,
multiplier: 1,
constant: 0)
self.addConstraint(constTrailing)
let constTop = NSLayoutConstraint(item: self.blueView!,
attribute: NSLayoutAttribute.Top,
relatedBy: .Equal,
toItem: self,
attribute: NSLayoutAttribute.TopMargin,
multiplier: 1,
constant: 0)
self.addConstraint(constTop)
let consBottom = NSLayoutConstraint(item: self.blueView!,
attribute: NSLayoutAttribute.Bottom,
relatedBy: .Equal,
toItem: self,
attribute: NSLayoutAttribute.BottomMargin,
multiplier: 1,
constant: 0)
self.addConstraint(consBottom)
}