我试图从一个扩展添加下面我UISegmentedControl一个子视图,所以当我添加更多的属性来此子视图可以从应用程序内的其他UISegmentedControls被重用。分段控制子视图从扩展
它出现在正确的地方,但由于某些原因有时会出现上述一些段,有时下面。我错过了什么吗?
所以在我的扩展到目前为止,这是我有:
import UIKit
extension UISegmentedControl {
func addBackgroundView() {
let backgroundView = UIView();
backgroundView.frame = self.bounds;
backgroundView.backgroundColor = UIColor.purple;
self.insertSubview(backgroundView, belowSubview: self);
}
}
然后,我称之为扩展功能从viewDidLoad方法中的视图控制器:
self.segmentedControlUnit.addBackgroundView();
尝试此链接:http://www.richardhsu.me/posts/2015/01/26/segmented-control.html – KKRocks
通过阅读答案中的评论,最好的解决方案是(1)子类的UIView而不是UISegmentedControl,(2)添加UISegementedControl作为一个子视图,(3)揭露那些你需要每个类的东西 - 的UIView和UISegmentedControl - (4)加入希望这些自定义的东西呢?或多或少,你是否可以通过反转*顺序和*子类*而不是*扩展*来获得所需的结果? – dfd