简体   繁体   English

使用Swift在UIView中未显示UIButton

[英]UIButton not showing in UIView using Swift

I am trying to create a view that stays at the bottom of the screen. 我试图创建一个停留在屏幕底部的视图。 The UIView works correctly and displays. UIView可以正常工作并显示。 I am trying to place a UIButton the size of the UIView in it, but the button just will not show. 我试图在其中放置一个UIView大小的UIButton,但是该按钮将不会显示。 I placed this code in the Swift Playground and it worked! 我将这段代码放在Swift Playground中,它起作用了! When I run it in the iOS simulator in my app the UIView displays but not the button. 当我在应用程序的iOS模拟器中运行它时,UIView会显示,但不会显示按钮。 I have no idea what is going on. 我不知道发生了什么。

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)

I got it. 我知道了。 I was using the frame and not the bounds. 我使用的是框架,而不是边界。 Using the frame would place it incorrectly, but using the bounds made it work. 使用框架会错误地放置它,但是使用边界会使它起作用。

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