简体   繁体   中英

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. I am trying to place a UIButton the size of the UIView in it, but the button just will not show. I placed this code in the Swift Playground and it worked! When I run it in the iOS simulator in my app the UIView displays but not the button. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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