我正在按照本教程创建评级按钮 - https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson5.html
我通过拖放在Storyboard内部的UIViewController中创建了一个UIView,并且UIView由下面的类控制。
class RatingControl: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "ratingButtonTapped:", forControlEvents: .TouchDown)
addSubview(button)
}
override func intrinsicContentSize() -> CGSize {
return CGSize(width: 320, height: 44)
}
func ratingButtonTapped(button: UIButton) {
print("Button pressed ")
}
}
问题是当我添加UIButton时,除了UIView中的一个按钮之外,它还显示了两个按钮,其中一个位于其位置(x:0,y:0)的父UIViewController。我希望按钮只显示在UIViewController内部的UIView中。
我研究了防止它的方法,一种方法是在故事板中手动添加这些按钮,而不是在init中创建它们,但后来我很好奇它在教程中是如何工作的。我使用的是iOS 8.3。
截图: