Custom UIView Class - Swift

后端 未结 1 565
天涯浪人
天涯浪人 2021-02-20 02:48

I\'ve builded a view which appears from the bottom and hides after sometime and it works well,but i want to make it in UIView class as a modal, i looked over the in

1条回答
  •  臣服心动
    2021-02-20 03:02

    code:

     var label:UILabel!
    var button:UIButton!
    
    override init (frame : CGRect) {
        super.init(frame : frame)
        self.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
    
    
        label = UILabel(frame: CGRect(x: 12, y: 8, width: self.frame.size.width-90, height: 50))
        label.text = "Connection error please try again later!!"
        label.textColor = UIColor.whiteColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        self.addSubview(label)
    
        button = UIButton(frame: CGRect(x: self.frame.size.width-87, y: 8, width: 86, height: 50))
        button.setTitle("OK", forState: UIControlState.Normal)
        button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
        button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
        self.addSubview(button)
    }
    

    and use it:

    let snakeView = snake(frame: CGRectMake(0 ,self.view.frame.size.height-66, self.view.frame.size.width, 66)))
    

    and set data for snakeview:

    snakeView.label.text = "hello"
    

    But usually i 'll create a function to update data for view:

    func updateData(title:String){
        self.label.text = title
    }
    

    and call it when need:

    snake.updateData("hello")
    

    P/s: if you use xib, you must to implement awakeFromNib instead of init. And create snake with xib(remember set identifier for xib : "snakeView"):

    let snakeView = NSBundle.mainBundle().loadNibNamed("snakeView", owner: nil, options: nil)[0] as snakeView
    

    0 讨论(0)
提交回复
热议问题