您好,我在設計UISegmentControl的時候在swift 3.0中通過添加目標來編程,但請幫助我。如何以編程方式在swift 3.0中添加UISegmentedControl?
-2
A
回答
7
嘗試此鏈接:
ADDING A SEGMENTED CONTROL PROGRAMMATICALLY
更新時間:
import UIKit
class ViewController: UIViewController {
/**
Loads the view and in our case we override default loadView to provide
custom SegmentedControl.
*/
override func loadView() {
super.loadView()
self.view.backgroundColor = UIColor.purpleColor()
println("Main view's loadView() called.")
// Initialize
let items = ["Purple", "Green", "Blue"]
let customSC = UISegmentedControl(items: items)
customSC.selectedSegmentIndex = 0
// Set up Frame and SegmentedControl
let frame = UIScreen.mainScreen().bounds
customSC.frame = CGRectMake(frame.minX + 10, frame.minY + 50,
frame.width - 20, frame.height*0.1)
// Style the Segmented Control
customSC.layer.cornerRadius = 5.0 // Don't let background bleed
customSC.backgroundColor = UIColor.blackColor()
customSC.tintColor = UIColor.whiteColor()
// Add target action method
customSC.addTarget(self, action: "changeColor:", forControlEvents: .ValueChanged)
// Add this custom Segmented Control to our view
self.view.addSubview(customSC)
}
/**
Handler for when custom Segmented Control changes and will change the
background color of the view depending on the selection.
*/
func changeColor(sender: UISegmentedControl) {
println("Change color handler is called.")
print("Changing Color to ")
switch sender.selectedSegmentIndex {
case 1:
self.view.backgroundColor = UIColor.greenColor()
println("Green")
case 2:
self.view.backgroundColor = UIColor.blueColor()
println("Blue")
default:
self.view.backgroundColor = UIColor.purpleColor()
println("Purple")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
相關問題
- 1. 以編程方式在UIToolbar中添加UISegmentedControl UINavigationBar
- 2. 如何在Swift 3中以編程方式向UICollectionViewCell添加UILabel
- 3. 如何以編程方式顯示UISegmentedControl?
- 4. 如何以編程方式在iPad上的工具欄中添加UISegmentedControl
- 5. Swift:如何安排以編程方式添加的複選框?
- 6. 以編程方式填充使用swift的UISegmentedControl
- 7. 以編程方式在Swift中添加UIBarButtonItem動作
- 8. IOS以編程方式在Swift中添加約束條件
- 9. 以編程方式在swift中添加「垂直間距」
- 10. 在SWIFT OS X中以編程方式添加和刪除NSTextField
- 11. 以編程方式在swift中添加全尺寸視圖
- 12. Swift:以編程方式在鍵盤上方添加UIButton
- 13. 以任何方式在.NET中以編程方式添加HttpHandler?
- 14. 如何以編程方式添加Slidingpanelyout?
- 15. 如何以編程方式添加ZedGraph?
- 16. 如何以編程方式添加UIImage?
- 17. 如何以編程方式添加DataTrigger
- 18. 如何以編程方式添加UIPanGestureRecognizer
- 19. 如何以編程方式添加UITabBarItem?
- 20. 如何以編程方式添加UIBarButtonItem?
- 21. 如何以編程方式添加ProgressBar?
- 22. 想知道如何以編程方式在Swift 3.0中繼承UITableViewCell?
- 23. 以編程方式更改UISegmentedControl值
- 24. UISegmentedControl以編程方式重置
- 25. 以編程方式使用UISegmentedControl文本
- 26. 如何在android中以編程方式在snackbar中添加edittext?
- 27. 如何在iOS中以編程方式在UIScrollView中添加UIImageVIew?
- 28. 如何在Delphi中以編程方式在TDBGrid中添加行
- 29. 以編程方式將UIControl添加到Navbar標題中 - Swift
- 30. 以編程方式將類添加到Swift中的UIbutton
確定。如果您現在無法啓動您的應用或崩潰,請提供一些代碼。沒有人知道你的問題與UISegmentControl。 –
其實我試過這個客觀的c代碼,並將其轉換爲swift3.0但不工作 segmentedControl = UISegmentedControl(items:[「First」,「Second」,「Third」]) segmentedControl.frame = CGRectMake(60, 250,200,30) segmentedControl.selectedSegmentIndex = 0 segmentedControl.addTarget(個體經營,行動: 「segmentedControlAction:」 forControlEvents:.ValueChanged) self.view.addSubview(segmentedControl) – Shital