[英]UIControlState in Swift
這是我的代碼:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
我有一個錯誤“在最后一行找不到成員'Normal'。怎么了?
您需要將attributes
為[NSObject : AnyObject]
,您的代碼將為:
segmentedControl.setTitleTextAttributes(attributes as [NSObject : AnyObject]?, forState: .Normal)
這是Apple Docs的默認語法:
func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, forState state: UIControlState)
或者您可以將attributes
[NSObject : AnyObject]?
為[NSObject : AnyObject]?
當您創建它時,代碼將是:
let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: [NSObject : AnyObject]? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
更改代碼的以下行:
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)
對此:
self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.normal)
或者您也可以使用以下命令:
self.segmentedControl.setTitleTextAttributes(attributes as? [AnyHashable : Any], forState: UIControlState.normal)
它應該可以處理您的代碼。 您只需重新啟動xcode。
嘗試打擊代碼:
import UIKit
class ViewController: UIViewController {
@IBOutlet var segment: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
let font = UIFont(name: "AvenirNext-Regular", size: 26.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
segment.setTitleTextAttributes(attributes! as [NSObject : AnyObject], forState:.Normal)
// 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.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.