Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Simple question here. Here's what I have:. Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change? Just a clarification for those new to Swift and iOS programming. Below line of code:.
So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play to Pause based on a toggle variable, you need to also create an IBOutlet for that button. If you try to use button. Its obvious once you know it, but for the noobs we all were this is a helpful tip.
If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one. To set a title for a button in Xcode using swift - first create a method called setTitle with parameter title and UIController state like below. Learn more. Asked 5 years, 6 months ago. Active 2 months ago. Viewed k times. Here's what I have: currencySelector. Active Oldest Votes. In Swift 3, 4, 5: button. Patrick 1, 6 6 gold badges 16 16 silver badges 27 27 bronze badges.
Gal Marom Gal Marom 7, 1 1 gold badge 14 14 silver badges 19 19 bronze badges. You can omit UIControlState. Swift3 is now. Jones Oct 18 '16 at Swift3: self. Below line of code: button. Bendrix Bendrix 6 6 silver badges 10 10 bronze badges.
Yep, I was about to google exactly this until I saw your answer.Connect: Please click Connect. And: Click on the circle and rag it also to your ViewController. We need to connect an action. Here: The actionTriggered func appears in our ViewController. In it we will add some Swift statements. A button is tapped. Something happens—an action is taken, a Swift func is called. In Xcode we begin by dragging a Button to our window. Next, please find the Control key on your keyboard and press it.
Drag the Button from your iPhone window to the ViewController file. Add outlet. A window will appear—here you type in the name you want to reference the Button by in your Swift code. I suggest "numberButton" for this tutorial, but any name is OK. Add title. Next please set a title for the button—this tutorial requires a numeric title like " Primary action triggered. Now go back to the Button and right-click on it.
Another pop-up will appear. Find the circle near the Primary Action Triggered item. Connect action. In the next window that appears we need to name the action func.
I suggest "actionTriggered" because it is easy to remember what that references. Add code. This is the interesting part. We want to add some code to our "actionTriggered" func. This code accesses and changes the UIButton's title. CurrentTitle: This is an optional string. We use an "if let" statement to access its value safely. Int: We parse the Button's title as an integer with the Int func. We check the optional result in an if-let statement. SetTitle: We call setTitle with the string returned by the String func.
We use UIControlState. Normal for the forState argument. Get title of Button.In order to change color of navigation bar for all view controllers, you have to set it in AppDelegate. In here tintColor attribute change the background color of the navigation bar.
First define the property of view controller-based status bar property in your info. This function converts hex values to UIColor with red, green and blue. I added in viewDidLoad. I'm trying to change navigation bar color but in my AppDelegate. It's passed in to the application function. So where do I add the code to customize the navigation bar.
You used a variable and named it navigationBarAppearace and you meant navigationBarAppeara n ce. Not tint color. November 16, Last Updated: November 16, Customize navigation bar appearance with swift swift ios8 navigationbar navigation-bar-appearace ios. Change navigation bar color In order to change color of navigation bar for all view controllers, you have to set it in AppDelegate.
Written by eranga bandara. August 17, I added in viewDidLoad self. August 27, In enums in Swift, you can just use the value without specifying the name of the enum Like so, UIApplication. November 11, December 09, February 05, Could you provide an example with a blurred background on the navigation bar? April 07, May 09, August 01, October 02, March 11, November 07, March 08, Sponsored by.Posted on June 22, by Steven Lipton.
Go to the storyboard and click the view controller icon in the scene. Drag two buttons, one labeled Pizza and the other labeled Pasta out to the scene. Set the font size on both to 26 Point. Arrange them like this on the storyboard:. Select the Pizza button. Pin the Pizza button 0 points up, 0 left, 0 right, and 0 down like this:. Add the four constraints without updating. Select the Pasta button.
Add the four constraints. Now Control-drag from the Pizza button to the Pasta button. Select Equal Widths in the menu that appears. Drag two more view controllers on to the storyboard. Control drag from the Pizza button to the red scene. Select a show segue. In the attributes inspector, set the segue identifier to pizza. Control-drag from the Pasta button to the blue scene.
In the properties inspector, set the segue identifier to pasta.
How to add UIButton programmatically in SWIFT 4.0
Your storyboard should look like this:. In this lesson we will do all the coding in ViewControllerso there is no need of code in the two new controllers. When used with navigation controllers, this controls the navigation bar at the top of the view. The navigationItem property is an instance of UINavigationItemwhich has four major properties: a title, a left bar button, a right bar button and a prompt.
To set the title on the toolbaryou set the string for the title property. For example add this to the ViewController class. We used viewWillAppear and not viewDidLoad. ViewController is the root view controller in the navigation stack. It only loads once and stays active in the background.Posted on April 18, by Steven Lipton. Story boards are great — most of the time. Where they fail miserably is in dynamic placement of controls such as buttons.
Some applications will change the number or position of buttons due to user input. In those cases, you need to use a programmatic button instead of a storyboard. We can get very complicated very quickly with the layout of buttons. Check out the links above to learn how to use these other methods. Save the project and go to the storyboard. Find the Vertical Stack View in the object library.
Drag a Vertical Stack View on the storyboard. Click the auto layout pin button and pin the stack view up 20 pointsdown 20 pointsleft 0 and right 0 points.
Update the frames with Items of New Constraints. Click the Add the 4 Constraints button.
A Beginner’s Guide to SwiftUI Buttons
Your stack view now covers much of the storyboard. Open the assistant editor in automatic mode so you see ViewController. Swift in the assistant editor. If you want to do this programmatically, see the lesson on Programmatic Stack Views for guidance.
Close the assistant editor. Go to the ViewController class. Change the background of the view for the controller to dark gray. Change the viewDidLoad method to this:. The function returns a UIButtonbut one without configuration. We create a system button called my button and return it. The rest of our code configures our button.
Swift UIButton, iOS Example
If we want to explicitly place the button somewhere on-screen, we will have to set the frame for the button. Change the code to this:.
Our button will appear with its upper left corner at 30 across and 30 down from the upper left corner of the device with a width of and a height of UIButtons have a transparent background by default. We can change the background color of the button to show the rectangle of the button. We can have a visible button now. The code above works for any UIView. However if you were to build and run now, you would still see nothing. When you use controls programmatically, you have to add the controls to the controllers view.
How to Make Dynamic UIButtons in Swift
The view.How to in Swift change UIButton text? A common mistake, also done by myself some time ago, is to try to change it through an IBAction. You can use an IBAction, but probably not the way you think. A common mistake is to do something like this:. This will give a compiler error if you try something like that.
I would make sense to be able to do it like that. Like in Visual Studio you can change text like that. In the first method, we will use an IBOutlet to change the button text.
First, create a UIButton somewhere on the storyboard. Second, create the IBOutlet the normal way. And you should end up with something like this:. Now in the viewDidLoad function, we will try to set a new title for the button. Try put in this into the viewDidLoad function:. If you try to run your code in the simulator now, your app will start up with the button called Ready.
The text from the storyboard should never even appear in your app. I have pasted the complete source code from the example below. In some cases, it might be more practical to use an IBOutlet, if your button should change text without any user interaction. If you want the text to change after the user clicked the button, the IBOutlet is not needed. You can do that from the IBAction directly. Here is how. Notice the first word in the parenthesis. It says sender, or at least it should say.
That sender represents the UIButton. And you can use the same setTitle function directly with the sender. Check out this example. Try to run that example an see what happens.
You must be logged in to post a comment. Skip to content. A common mistake is to do something like this: button.
Try put in this into the viewDidLoad function: button. Normal If you try to run your code in the simulator now, your app will start up with the button called Ready. Tags: Swift text UIButton.There are likely thousands of possibilities for creating a custom button. For the purposes of this tutorial, I am only going to create one custom button. However, it will utilize several custom options that can go a long way to creating your own unique button. I created a variant of this button for my math game Scrath.
First, either create a new, single view project in Xcode or use an existing project. Select Cocoa Touch Class in the template menu. Now, select your new swift file named MyNewButton. Delete the commented section within the class. So, there are two possible functions we can use for our code: initWithCoder or awakeFromNib. The main difference between the two is that awakeFromNib is only run after all the connections to the nib have been loaded. I personally prefer to use awakeFromNib.
Now, the button we are creating has a border and rounded edges. It also has a gradient background color, and a matching text and border color. In terms of behavior, we will set a normal color, a disabled color and we will create an action that will run when the button is pressed.
I like to separate as much as I can into variables. That way I can change it in one place instead of hunting around in my code for all the mentions. The above may or may not be something you will want to code into your button class. I prefer to at least set a default size width and height for the button as that is something I usually want to remain consistent on all views. Plus, it is a huge hassle to set those constraints for every button only to be asked later by your client to try a different width.
Doing this in code makes life so much easier! Now, the origin is what you may want to modify or remove completely. To explain the code, the x point takes the width of the superview, divides it in half and subtracts it from half of the width of the button. In other words, the center of the button will be in the horizontal center of the view.
The y point returns the original y point of the button. CornerRadius determines the radius of the edges, giving our button the rounded edge we are looking for. Definitely play with this value! This is why we use clipsToBounds. Finally, borderWidth defines the width of the border and the color defines the color must be cgColor. Here, we are setting the color for two states: normal default state and disabled.
If you look at the variable, the disabled color is the same color as the normal state color, just with a lower transparency — typical of a disabled button. AdjustsFontSizeToWidth will reduce the font size until the text can fit inside the button without cropping. However, I use it occasionally when I change fonts and frame sizes based on device sizes.
Finally, I like to confirm my buttons will all have Title Case. That is why I use setTitle with.