【发布时间】:2017-04-25 22:24:39
【问题描述】:
我正在尝试通过扩展在我的 UISegmentedControl 下方添加一个子视图,因此当我向此子视图添加更多属性时,它可以从应用程序中的其他 UISegmentedControl 重用。
它出现在正确的位置,但由于某种原因,有时会出现在某些片段的上方,有时会出现在下方。我错过了什么吗?
到目前为止,在我的扩展中,这就是我所拥有的:
import UIKit
extension UISegmentedControl {
func addBackgroundView() {
let backgroundView = UIView();
backgroundView.frame = self.bounds;
backgroundView.backgroundColor = UIColor.purple;
self.insertSubview(backgroundView, belowSubview: self);
}
}
然后,我从 ViewController 中的 viewDidLoad 方法调用扩展函数:
self.segmentedControlUnit.addBackgroundView();
【问题讨论】:
-
通读答案中的 cmets,最好的解决方案是(1)子类 UIView 而不是 UISegmentedControl,(2)添加 UISegmentedControl 作为子视图,(3)公开你需要的那些东西每个类 - UIView 和 UISegmentedControl - 以及 (4) 添加您希望的自定义内容?或多或少,你能通过reversing顺序和subclassing而不是extending得到想要的结果吗?
标签: ios swift uisegmentedcontrol