lostAtSeaJoshua lostAtSeaJoshua - 4 years ago 169
Swift Question

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)

Answer Source

I got it. The frame for the button was not correct. Using theButton.Frame = theView.frame was making the button not display on frame.

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

I used that and it worked.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download