2014-11-06 104 views
0

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

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) 
+1

尝试用'searchButton'的'center'财产玩耍和看。我的猜测是你的按钮不在屏幕上。 – Unheilig 2014-11-06 15:33:59

+0

我只是累了,仍然没有运气。如果我添加一个UILabel它的作品。只是按钮不起作用。我感到很困惑! – lostAtSeaJoshua 2014-11-06 15:45:24

回答

1

我明白了。我使用的是框架而不是界限。使用框架会错误地放置它,但是使用边框会使其工作。

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

确切地说,我在哪里;-)很高兴它解决了。 – Unheilig 2014-11-06 15:49:56

+0

是的,你有关与中心玩的评论,谢谢! – lostAtSeaJoshua 2014-11-06 15:52:11

0

斯威夫特3.X +

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