UIView
In iOS, UIView class provides rectangular area in the view, this view can render user defined content on the iPhone screen. Here is the very basic example for creating UIView programmatically.
1 2 3 4 5 | var DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100)) DynamicView.backgroundColor=UIColor.greenColor() DynamicView.layer.cornerRadius=25 DynamicView.layer.borderWidth=2 self.view.addSubview(DynamicView) |
And your screen will have this :
UISlider
UISlider is the basic UI component that provides user with the option to select the point from continuous range of points.
It has a thumb(knob like thing) on it which indicates the current selected value. Here is the code to create UISlider programmatically.
1 2 3 4 5 6 7 8 | var sliderDemo = UISlider(frame:CGRectMake(20, 260, 280, 20)) sliderDemo.minimumValue = 0 sliderDemo.maximumValue = 100 sliderDemo.continuous = true sliderDemo.tintColor = UIColor.redColor() sliderDemo.value = 50 sliderDemo.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged) self.view.addSubview(sliderDemo) |
We can also add the function that will be called when user slides the thumb of slider, for getting the current value corresponding to the position of the thumb:
1 2 3 4 | func sliderValueDidChange(sender:UISlider!) { println("value--\(sender.value)") } |
Here is how your slider will look like :
UISwitch
We use UISwitch when we want to give only two option to the user.
For example In setting page we can implement sound On/Off option, vibration On/Off etc.
iOS by default provide this option by default and it can be used as the alternative to check box.
Here is the code snippet for creating and using the UISwitch Programmatically.
1 2 3 4 5 | var switchDemo=UISwitch(frame:CGRectMake(150, 300, 0, 0)); switchDemo.on = true switchDemo.setOn(true, animated: false); switchDemo.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged); self.view.addSubview(switchDemo); |
and for getting the triggered when switch is turned on.off by the user, we write following function
func switchValueDidChange(sender:UISwitch!)
{
if (sender.on == true){
println(“on”)
}
else{
println(“off”)
}
}