Wednesday, 21 February 2018

Creating Button, Label, TextField & ImageView when Click the Button - Swift

Creating Button, Label, TextField & ImageView when Click the Button - Swift

class HomeViewController: UIViewController, UITextFieldDelegate
{
var btn_navbar:UIButton!
@IBOutlet var lbl_one: UILabel!
var image_View:UIImageView!
@IBOutlet var txt_field: UITextField!

override func viewDidLoad()
    {
        self.title = "Home View Controller"

//Button Code
        btn_navbar = UIButton.buttonWithType(UIButtonType.System) as UIButton
        btn_navbar.frame = CGRect(x: 280, y: 80, width: 40, height: 40)
        btn_navbar.setTitle("Show/Hide", forState: UIControlState())
        btn_navbar.addTarget(self, action: "showhide", forControlEvents: UIControlEvents.TouchUpInside)
        btn_navbar.backgroundColor = UIColor.lightGrayColor()
        self.view.addSubview(btn_navbar)
    }

func showhide()
    {
        if(self.navigationController?.navigationBar.hidden == true)
        {
           self.navigationController?.navigationBar.hidden=false
        }
        else
        {
            self.navigationController?.navigationBar.hidden=true
        }
    }

@IBAction func load_img(sender: AnyObject)
    {
        //Find the Button Tag
        var btn:UIButton=sender as UIButton
        println(btn.tag)
       
        if(btn.tag == 1)
        {
           self.loadImage()
        }
        else if(btn.tag == 2)
        {
            self.loadLabel()
            [self .showTextField()]
        }
    }

//Creating Image View
func loadImage()
    {
        image_View = UIImageView(frame: CGRect(x: 60, y: 500, width: 300, height: 120))
        image_View.image = UIImage(named: "test.jpg");
        image_View.userInteractionEnabled=true
        self.view?.addSubview(image_View)
    }
   
//Creating Label
    func loadLabel()
    {
        lbl_txt=UILabel(frame: CGRect(x: 49, y: 184, width: 224, height: 21))
        lbl_txt.text = "button clicked"
        lbl_txt.textColor = UIColor.orangeColor()
        lbl_txt.textAlignment = NSTextAlignment.Right
    }

//Creating Text Field
func showTextField()
    {
        txt_field = UITextField(frame: CGRect(x: 80, y: 300, width: 200, height: 30))
        txt_field.layer.borderColor = UIColor.blackColor().CGColor
        txt_field.layer.borderWidth = 1.2
        txt_field.placeholder = "Text Me"
        txt_field.textColor = UIColor.brownColor()
        txt_field.textAlignment = NSTextAlignment.Center
        txt_field.delegate = self
        self.view?.addSubview(txt_field);
    }
   
//Return Key is Pressed from Keyboard
    func textFieldShouldReturn(textField: UITextField!) -> Bool
    {
        txt_field.resignFirstResponder()
        return true;
    }
   
    //Background Tabs Keyboard is hide
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
    {
        txt_field.resignFirstResponder()
    }
}

Find the Button Tag - Swift
@IBAction func Btn_Click(sender: AnyObject)
{
//Find the Button Tag
    var btn:UIButton=sender as UIButton
    println(btn.tag)
}

No comments: