Swift中的UIControlState

时间:2015-06-01 11:43:12

标签: swift uicontrolstate

这是我的代码:

let font = UIFont(name: "AvenirNext-Regular", size: 16.0)
let attributes: NSDictionary? = [ NSFontAttributeName : font! ]
self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)

我有一个错误“在最后一行找不到成员'正常'。出了什么问题?

3 个答案:

答案 0 :(得分:2)

您需要将attributes投射到[NSObject : AnyObject],您的代码将是:

segmentedControl.setTitleTextAttributes(attributes as [NSObject : AnyObject]?, forState: .Normal)

这是Apple Docs的默认语法:

func setTitleTextAttributes(_ attributes: [NSObject : AnyObject]?, forState state: UIControlState)

或者您可以在创建attributes时将[NSObject : AnyObject]?投射到let font = UIFont(name: "AvenirNext-Regular", size: 16.0) let attributes: [NSObject : AnyObject]? = [ NSFontAttributeName : font! ] self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.Normal) ,代码将为:

C

答案 1 :(得分:0)

更改代码的以下行:

self.segmentedControl.setTitleTextAttributes(attributes, forState:.Normal)

到此:

self.segmentedControl.setTitleTextAttributes(attributes, forState: UIControlState.normal)

或者您也可以使用以下内容:

self.segmentedControl.setTitleTextAttributes(attributes as? [AnyHashable : Any], forState: UIControlState.normal)

答案 2 :(得分:0)

它应该适用于您的代码。你只需重新启动你的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.
    }

}