Make a UIButton programmatically in Swift

后端 未结 19 1823
死守一世寂寞
死守一世寂寞 2020-11-27 10:00

I am trying to build UI\'s programmatically. How do I get this action working? I am developing with Swift.

Code in viewDidLoad:

over         


        
19条回答
  •  庸人自扰
    2020-11-27 10:41

    UIButton with constraints in iOS 9.1/Xcode 7.1.1/Swift 2.1:

    import UIKit
    import MapKit
    
    class MapViewController: UIViewController {  
    
        override func loadView() {
            mapView = MKMapView()  //Create a view...
            view = mapView         //assign it to the ViewController's (inherited) view property.
                                   //Equivalent to self.view = mapView
    
            myButton = UIButton(type: .RoundedRect)  //RoundedRect is an alias for System (tested by printing out their rawValue's)
            //myButton.frame = CGRect(x:50, y:500, width:70, height:50)  //Doesn't seem to be necessary when using constraints.
            myButton.setTitle("Current\nLocation", forState: .Normal)
            myButton.titleLabel?.lineBreakMode = .ByWordWrapping  //If newline in title, split title onto multiple lines
            myButton.titleLabel?.textAlignment = .Center
            myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            myButton.layer.cornerRadius = 6   //For some reason, a button with type RoundedRect has square corners
            myButton.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5) //Make the color partially transparent
            //Attempt to add padding around text. Shrunk the frame when I tried it.  Negative values had no effect.
            //myButton.titleEdgeInsets = UIEdgeInsetsMake(-10,-10,-10,-10)
            myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)  //Add padding around text.
    
            myButton.addTarget(self, action: "getCurrentLocation:", forControlEvents: .TouchUpInside)
            mapView.addSubview(myButton)
    
            //Button Constraints:
            myButton.translatesAutoresizingMaskIntoConstraints = false //***
            //bottomLayoutGuide(for tab bar) and topLayoutGuide(for status bar) are properties of the ViewController
            //To anchor above the tab bar on the bottom of the screen:
            let bottomButtonConstraint = myButton.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -20) //Implied call of self.bottomLayoutGuide. Anchor 20 points **above** the top of the tab bar.
            //To anchor to the blue guide line that is inset from the left 
            //edge of the screen in InterfaceBuilder:
            let margins = view.layoutMarginsGuide  //Now the guide is a property of the View.
            let leadingButtonConstraint = myButton.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
    
            bottomButtonConstraint.active = true
            leadingButtonConstraint.active = true
        }
    
    
        func getCurrentLocation(sender: UIButton) {
            print("Current Location button clicked!")
        }
    

    The button is anchored to the bottom left corner, above the tab bar.

提交回复
热议问题