ios - 在 Swift 中以编程方式创建 UITextView 时未获得输出

标签 ios iphone swift

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

}

ViewController.swift

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.
}
}

UIViewUsingTextField.swift

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
}
*/

}

通过使用上面的代码,我无法在我的模拟器中获得蓝色的 UITextView。我想以编程方式创建 UITextview,我找不到实际问题,即为什么我没有将 Blue UITextView 作为此代码的输出。作为此代码的输出,我的模拟器中只有白屏。我尝试按照 this 编码关联。谁能帮我解决这个问题以获得所需的输出。

最佳答案

将自定义类设置为 View :

View class

下面的代码将在 View 的中心添加一个 200x200 的 blueView:

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])
    }
}

如果您希望您的 blueView 覆盖整个 View :

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)
}

关于ios - 在 Swift 中以编程方式创建 UITextView 时未获得输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39507053/

相关文章:

iphone - AUGraphInitialize 将kAudioUnitSubType_Reverb2 添加到AUGraph 时出现错误代码-10868

c# - 错误 : Namespace "iOS" does not exist in Unity. 通知

ios - 在 Xcode 中激活自定义字体的目标成员身份

ios - 使用后台队列从 url 加载图像仍然不够快

iphone - jQuery 移动页面在 Safari 移动中 super 大

iphone - 是否允许在单例类中为变量定义属性和综合?

ios - Swift2 - 在嵌套 JSON 中使用 Alamofire 3

swift - 使用导航栏在 Swift 中的 ViewController 之间存储数据

ios - 更改 SpriteKit 节点的纹理

IOS swift : Declaration is only valid at file scope