

How To Add Button, Label In iOS App Programmatically. Now run the example again, you will see below demo video ( ios button click event change label text example ).Below is the demo video of this process ( embbed label and button into stack view and add stackview constraints to layout it in the screen center ).Select the newly created StackView and click Align icon at the bottom right to add constraints to layout the StackView stay at screen center in both directions. Click the Embed In icon at the bottom right bar to put the button and label into a StackView.Select both the button and label together.And then you just need to add constraints to the StackView object to create the layout. StackView is a container view, it can group some child view together and freeze them to a block.
#Apple icon plus uibutton code
Now all the source code has been added complete, you can run the example, but when you start it, you will find the button and label layout may not look well, so we need to use the StackView component to group the button and label together to make it’s layout beautiful.Use Stack View To Group Button And Label. Do any additional setup after loading the view, typically from a func onClick(_ sender: UIButton, forEvent event: UIEvent) 3. Input below source code in the ViewController class source file ViewController.swift.Input name label and click Connect button to add a UILabel type instance variable in the ViewController source code. Click the label view component and press the Control key at the same time, and drag the label to the right side ViewController source code.Below is the demo video of this process ( add label related instance variable in view controller class).But how to references the label in the source code, so that we can change its text when the button is clicked. Now you have added a swift button action process method to handle the button click event, you can write code in the onClick method.2.2 Add Label Referral Instance Variable. Click Connect button, there will add a method in the ViewController source code.

In the Arguments drop-down list, you can select Sender or Sender And Event. Select Touch Up Inside in the event drop-down list.
