Swift / 如何设置addSubview为Subview?
Swift / How to set addSubview to Subview?
我想在我的 googleAdBanner-Subview 上添加一个 Subview。 (作为自定义关闭 UIButton)。
如果我将 UIButton 作为子视图添加到 self.view.addSubview(btn),它可以正常工作。但由于加载广告可能需要一段时间,因此有时 UIButton 可见,即使 googleAdBanner 仍然不可见。
如果我将 UIButton 添加为 googleAdBanner 的子视图,UIButton 将不会显示。
override func viewDidLoad() {
super.viewDidLoad()
let googleAdBanner = GADBannerView(frame:CGRectMake(0,self.view.frame.size.height - 50,self.view.frame.size.width,50))
googleAdBanner.adUnitID = "ca-app-pub-xx"
googleAdBanner.rootViewController = self
googleAdBanner.loadRequest(GADRequest())
self.view.addSubview(googleAdBanner)
let btn: UIButton = UIButton(frame: CGRectMake(self.view.frame.size.width - 25, self.view.frame.size.height - 50, 25, 25))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(RootVC.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1
googleAdBanner.addSubview(btn)
}
我错过了什么?非常感谢帮助。
使您的 'btn' 像这样:let btn: UIButton = UIButton(frame: CGRectMake(0,0, btnWidth, btnHeight))
将其作为子视图添加到横幅后,然后通过设置 CGrect 或自动布局调整按钮框架
我想在我的 googleAdBanner-Subview 上添加一个 Subview。 (作为自定义关闭 UIButton)。
如果我将 UIButton 作为子视图添加到 self.view.addSubview(btn),它可以正常工作。但由于加载广告可能需要一段时间,因此有时 UIButton 可见,即使 googleAdBanner 仍然不可见。
如果我将 UIButton 添加为 googleAdBanner 的子视图,UIButton 将不会显示。
override func viewDidLoad() {
super.viewDidLoad()
let googleAdBanner = GADBannerView(frame:CGRectMake(0,self.view.frame.size.height - 50,self.view.frame.size.width,50))
googleAdBanner.adUnitID = "ca-app-pub-xx"
googleAdBanner.rootViewController = self
googleAdBanner.loadRequest(GADRequest())
self.view.addSubview(googleAdBanner)
let btn: UIButton = UIButton(frame: CGRectMake(self.view.frame.size.width - 25, self.view.frame.size.height - 50, 25, 25))
btn.backgroundColor = UIColor.greenColor()
btn.setTitle("Click Me", forState: UIControlState.Normal)
btn.addTarget(self, action: #selector(RootVC.buttonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
btn.tag = 1
googleAdBanner.addSubview(btn)
}
我错过了什么?非常感谢帮助。
使您的 'btn' 像这样:let btn: UIButton = UIButton(frame: CGRectMake(0,0, btnWidth, btnHeight))
将其作为子视图添加到横幅后,然后通过设置 CGrect 或自动布局调整按钮框架