在Swift中以编程方式创建UITextView时未获得输出 [英] Not Getting Output on Creating UITextView Programmatically in Swift

查看:36
本文介绍了在Swift中以编程方式创建UITextView时未获得输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AppDelegate.swift

 导入UIKit@UIApplicationMain类AppDelegate:UIResponder,UIApplicationDelegate {var window:UIWindow?func application(应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?)->布尔{//应用程序启动后进行自定义的替代点.返回真}} 

ViewController.swift

 导入UIKit类ViewController:UIViewController {覆盖func loadView(){self.view = UIViewUsingTextField()}覆盖func viewDidLoad(){super.viewDidLoad()//加载视图后进行其他任何设置,通常是从笔尖进行.}覆盖func didReceiveMemoryWarning(){super.didReceiveMemoryWarning()//处理所有可以重新创建的资源.}} 

UIViewUsingTextField.swift

 导入UIKitUIViewUsingTextField类:UIView{var blueview:UIView?在里面() {super.init(框架:CGRectZero)self.backgroundColor = UIColor.whiteColor()self.setupView()}所需的init(coder aDecoder:NSCoder){fatalError("init(coder :)尚未实现")}覆盖init(frame:CGRect){super.init(frame:框架)}func setupView(){self.blueview = UIView()self.blueview?.backgroundColor = UIColor.blueColor()self.blueview?.translatesAutoresizingMaskIntoConstraints = falseaddSubview(self.blueview!)让centerXconstraint = NSLayoutConstraint(item:self.blueview !,属性:NSLayoutAttribute.CenterX,relatedBy:NSLayoutRelation.Equal,toItem:self,属性:NSLayoutAttribute.CenterX,乘数:1.0,常数:0)让centerYconstraint = NSLayoutConstraint(item:self.blueview !,属性:NSLayoutAttribute.CenterY,relatedBy:NSLayoutRelation.Equal,toItem:self,属性:NSLayoutAttribute.CenterY,乘数:1.0,常数:0)self.addConstraint(centerXconstraint)self.addConstraint(centerYconstraint)让widthConstraint = NSLayoutConstraint(item:self.blueview !,属性:NSLayoutAttribute.Width,relatedBy:NSLayoutRelation.Equal,toItem:nil,属性:NSLayoutAttribute.NotAnAttribute,乘数:1.0,常数:200)let heightConstraint = NSLayoutConstraint(item:self.blueview !,属性:NSLayoutAttribute.Width,relatedBy:NSLayoutRelation.Equal,toItem:nil,属性:NSLayoutAttribute.NotAnAttribute,乘数:1.0,常数:200)self.addConstraint(widthConstraint)self.addConstraint(heightConstraint)self.addConstraints([centerXconstraint,centerYconstraint,widthConstraint,heightConstraint])}/*//仅重写drawRect:如果执行自定义绘制.//空的实现会对动画期间的性能产生不利影响.覆盖func drawRect(rect:CGRect){//绘图代码}*/} 

通过使用以上代码,我无法在模拟器中获得蓝色的UITextView.我想以编程方式创建UITextview,我找不到实际的问题,为什么我没有将Blue UITextView作为此代码的输出.我在模拟器中只得到白色屏幕作为此代码的输出.我尝试按照

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

 导入UIKitUIViewUsingTextField类:UIView{var blueView:UIView吗?需要初始化吗?(编码器aDecoder:NSCoder){fatalError("init(coder :)尚未实现")}覆盖init(frame:CGRect){super.init(frame:框架)self.backgroundColor = UIColor.whiteColor()setupView()}func setupView(){self.blueView = UIView()self.blueView?.backgroundColor = UIColor.blueColor()self.blueView?.translatesAutoresizingMaskIntoConstraints = falseself.addSubview(self.blueView!)让blueViewCenterXConstraint = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.CenterX,relatedBy:NSLayoutRelation.Equal,toItem:self,属性:NSLayoutAttribute.CenterX,乘数:1.0,常数:0)让blueViewCenterYConstraint = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.CenterY,relatedBy:NSLayoutRelation.Equal,toItem:self,属性:NSLayoutAttribute.CenterY,乘数:1.0,常数:0)让blueViewWidthConstraint = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Width,relatedBy:NSLayoutRelation.Equal,toItem:nil,属性:NSLayoutAttribute.NotAnAttribute,乘数:1.0,常数:200)让blueViewHeightConstraint = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Height,relatedBy:NSLayoutRelation.Equal,toItem:nil,属性:NSLayoutAttribute.NotAnAttribute,乘数:1.0,常数:200)self.addConstraints([[blueViewCenterXConstraint,blueViewCenterYConstraint,blueViewWidthConstraint,blueViewHeightConstraint])}} 

如果您希望blueView覆盖整个视图:

  func setupView(){self.blueView = UIView()self.blueView?.backgroundColor = UIColor.blueColor()self.blueView?.translatesAutoresizingMaskIntoConstraints = falseself.addSubview(self.blueView!)让constLeading = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Leading,relatedBy:.等于,toItem:自我,属性:NSLayoutAttribute.Leading,乘数:1常数:0)self.addConstraint(constLeading)让constTrailing = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Trailing,relatedBy:.等于,toItem:自我,属性:NSLayoutAttribute.Trailing,乘数:1常数:0)self.addConstraint(constTrailing)让constTop = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Top,relatedBy:.等于,toItem:自我,属性:NSLayoutAttribute.TopMargin,乘数:1常数:0)self.addConstraint(constTop)让consBottom = NSLayoutConstraint(item:self.blueView !,属性:NSLayoutAttribute.Bottom,relatedBy:.等于,toItem:自我,属性:NSLayoutAttribute.BottomMargin,乘数:1常数:0)self.addConstraint(consBottom)} 

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

}

By using above code I am not abel to get the UITextView of Blue color in my simulator. I want to create UITextview Programmatically, I can not find the actual issue that why I am not getting the Blue UITextView as output of this code. I am getting only white Screen in my simulator as output of this code. I have tried to code as per this link. Will anybody please help me to fix this issue to have a desired output.

解决方案

Set the custom class to the View:

Below code will add an blueView of 200x200 at the center of the View:

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

If you want your blueView to cover whole 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)
}

这篇关于在Swift中以编程方式创建UITextView时未获得输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆