Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIView Class - Swift

Tags:

ios

swift

uiview

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 internet but i couldn't get or understand how to do that.

snake = UIView(frame: CGRect(x: 0 , y: self.view.frame.size.height-66, width: self.view.frame.size.width, height: 66))
snake.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)


let label = UILabel(frame: CGRect(x: 12, y: 8, width: snake.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)
snake.addSubview(label)

let button = UIButton(frame: CGRect(x: snake.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)
snake.addSubview(button)
self.view.addSubview(snake)

How to start with this class I've no idea, i am creating the view programmatically with its frame and i need to set properties for the button for example or the label and create the view from any class.

class snake: UIView {


    override init (frame : CGRect) {
        super.init(frame : frame)

    }

    convenience init () {
        self.init(frame:CGRect.zero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }

}
like image 567
Nata Mio Avatar asked Nov 26 '15 09:11

Nata Mio


1 Answers

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
like image 178
Nguyen Hoan Avatar answered Oct 13 '22 01:10

Nguyen Hoan