繁体   English   中英

使用Swift在UIView中未显示UIButton

[英]UIButton not showing in UIView using Swift

我试图创建一个停留在屏幕底部的视图。 UIView可以正常工作并显示。 我试图在其中放置一个UIView大小的UIButton,但是该按钮将不会显示。 我将这段代码放在Swift Playground中,它起作用了! 当我在应用程序的iOS模拟器中运行它时,UIView会显示,但不会显示按钮。 我不知道发生了什么。

var ySize = UIScreen.mainScreen().bounds.size.height - 40
let searchView = UIView(frame: CGRect(x: 0, y: ySize, width: self.view.bounds.width, height: 40))
searchView.backgroundColor = UIColor(red: 0/255.0, green: 71/255.0, blue: 133/255.0, alpha: 1)

let searchButton: UIButton = UIButton.buttonWithType(.System) as UIButton
searchButton.frame = searchView.frame
searchButton.tintColor = UIColor.whiteColor()
searchButton.setTitle("Search Transactions", forState: .Normal)

searchView.addSubview(searchButton)

self.navigationController?.view.addSubview(searchView)

我知道了。 我使用的是框架,而不是边界。 使用框架会错误地放置它,但是使用边界会使它起作用。

searchButton.frame = CGRectMake(0, 0, recentView.bounds.width, recentView.bounds.height)

迅捷3.x +

searchButton.frame = CGRect.init(x: 0.0, y:0.0, width:200.0, height:50.0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM