도래울

UIView, UISlider, UISwitch Swift 코드 생성 본문

개발/iOS

UIView, UISlider, UISwitch Swift 코드 생성

도래울 2016. 4. 29. 11:44

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(100200100100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
self.view.addSubview(DynamicView)

 

And your screen will have this :

UIView_programmatically1

 

 

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(2026028020))
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 :

UISlider-programmatically

 

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(15030000));
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”)
}
}

Finally our Custom switch will look like this :
UISwitch-programmatically


'개발 > iOS' 카테고리의 다른 글

UIView frame, bounds 그리고 좌표  (0) 2016.05.04
swift navigation bar image  (0) 2016.05.02
[iOS]NSLayoutConstraint 사용하기  (0) 2016.04.28
UIScrollview 페이징 처리  (0) 2016.04.27
UITableView에서 header와 footer 사용 하기  (0) 2016.04.26
Comments