设置全局分段控制的大小?

时间:2015-08-03 14:15:34

标签: ios swift ios8 uisegmentedcontrol

目前在我的每个视图中,我都在viewDidLoad事件中设置我的分段控件的高度,如下所示:

mySegmentedControl.frame = CGRect(
  x: mySegmentedControl.frame.origin.x,
  y: mySegmentedControl.frame.origin.y,
  width: mySegmentedControl.frame.size.width,
  height: 22)

它有效,但它很乏味,希望我能在AppDelegate.didFinishLaunchingWithOptions中做到这一点:

var frame = UISegmentedControl.appearance().frame
UISegmentedControl.appearance().frame = CGRect(
  x: frame.origin.x,
  y: frame.origin.y,
  width: frame.size.width,
  height: 22)

但是这没有效果甚至是错误,它仍然是默认高度。我甚至试图像这样继承它,但没有效果或错误:

class SegmentedControl: UISegmentedControl {
    override func sizeThatFits(size: CGSize) -> CGSize {
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = 22
        return sizeThatFits
    }
}

我有什么遗漏或全球设定高度的方法吗?

1 个答案:

答案 0 :(得分:0)

也许是这样的:

UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes 
                                forState:UIControlStateNormal];