How do I create a basic UIButton programmatically?

后端 未结 30 1220
清歌不尽
清歌不尽 2020-11-22 14:41

How can I create a basic UIButton programmatically? For example in my view controller, when executing the viewDidLoad method, three UIButton<

30条回答
  •  不知归路
    2020-11-22 15:25

    Objective-C

    UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
     [but setFrame:CGRectMake(52, 252, 215, 40)];
    [but setTitle:@"Login" forState:UIControlStateNormal];
    [but setExclusiveTouch:YES];
    
     // if you like to add backgroundImage else no need
       [but setbackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:but];
    
    -(void) buttonClicked:(UIButton*)sender
     {
    NSLog(@"you clicked on button %@", sender.tag);
     }
    

    Swift

        let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
        myButton.setTitle("Hai Touch Me", forState: .Normal)
        myButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        myButton.frame = CGRectMake(15, 50, 300, 500)
        myButton.addTarget(self, action: "pressedAction:", forControlEvents: .TouchUpInside)
    
        self.view.addSubview( myButton)
    
    
    func pressedAction(sender: UIButton!) {
       // do your stuff here 
      NSLog("you clicked on button %@", sender.tag)
    }
    

    Swift3 and above

      let myButton = UIButton() // if you want to set the type use like UIButton(type: .RoundedRect) or UIButton(type: .Custom)
        myButton.setTitle("Hi, Click me", for: .normal)
        myButton.setTitleColor(UIColor.blue, for: .normal)
        myButton.frame = CGRect(x: 15, y: 50, width: 300, height: 500)
    
        myButton.addTarget(self, action: #selector(pressedAction(_:)), for: .touchUpInside)
        self.view.addSubview( myButton)
    
    
    
    func pressedAction(_ sender: UIButton) {
       // do your stuff here 
      print("you clicked on button \(sender.tag)")
    }
    

    SwiftUI

    for example you get the step by step implemntation from SwiftUI Developer portal

    import SwiftUI
    
    struct ContentView : View {
        var body: some View {
            VStack {
    
                Text("Target Color Black")
                 Button(action: { 
                     /* handle button action here */ })
                {
             Text("your Button Name")
              .color(.white)
                            .padding(10)
                            .background(Color.blue)
                            .cornerRadius(5)
                            .shadow(radius: 5)
                            .clipShape(RoundedRectangle(cornerRadius: 5))
         }
    
    
            }
        }
    }
    
    #if DEBUG
    struct ContentView_Previews : PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    #endif
    

提交回复
热议问题