0

这是我的代码:

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

我有一个错误“在最后一行找不到成员‘正常’。怎么了?

4

3 回答 3

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)
于 2015-06-01T11:56:57.093 回答
0

更改代码的以下行:

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

对此:

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

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

self.segmentedControl.setTitleTextAttributes(attributes as? [AnyHashable : Any], forState: UIControlState.normal)
于 2015-06-01T11:53:08.310 回答
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.
    }

}
于 2015-06-01T12:28:28.370 回答