2014-11-06 48 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)