Swift- How to Display Image over Button?
I think your UIButton needs to be of type UIButtonTypeCustom, rather than UIButtonTypeSystem. You either constructed it as such, or need to update the UIButton's properties in interface builder.
More info: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/clm/UIButton/buttonWithType:
How to Display Image over Button? -Swift
I nailed it. I changed the button type to System button and the color changed to blue. However i managed to change the Blue color to white by setting the tint color to white which apple uses by default to set the color to its controls(i guess). So the system button color to blue was due to the reason the tintcolor was assigned as blue
let btnSort = UIButton.buttonWithType(UIButtonType.System) as UIButton
btnSort.frame = CGRectMake(2, 74, 140, 26)
btnSort.tintColor = UIColor.whiteColor()
btnSort.setImage(UIImage(named:"drop_arrow"), forState: UIControlState.Normal)
btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: 100,bottom: 6,right: 14)
btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: -30,bottom: 0,right: 34)
btnSort.setTitle("SORT", forState: UIControlState.Normal)
btnSort.layer.borderWidth = 1.0
btnSort.layer.borderColor = UIColor.whiteColor().CGColor
btnSort.addTarget(self, action: Selector("showSortTbl"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnSort)
Displaying Image On Button Swift
Assuming what you have as Button
is actually a UIButton
instance, it isn't enough to just pass name for an image, you actually need to pass in an actual UIImage:
let image = UIImage.named("ImageName")
myButton.setBackgroundImage(image!, forState: .Normal)
How to display an image when button is pressed in iOS?
You should drag and drop UIButton
from storyBoard to your code and use Action
to have an IBAction
in your viewController.
After that use imageView.isHidden = false
to show the image and vice versa.
sample code:
import UIKit
class ViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBAction func someButton(_ sender: UIButton) {
// when button tapped this code will execute
imageView.isHidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.isHidden = true
}
}
How to show different images in uimageview on button click
Create a property of your ViewController
called number
to keep track of the current number
. Increment it in the @IBAction
and set it back to 1
when it reaches 27
:
class ViewController : UIViewController {
@IBOutlet var imageView: UIImageView!
var number = 1
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "card1")
}
@IBAction func buttonPressed(_ sender: UIButton) {
// use % (mod function) to keep result in the range 1...26
number = number % 26 + 1
imageView.image = UIImage(named: "card\(number)")
}
}
Alternatively, you could simply have your button action increment number
and use a property observer on number
to update the image
:
class ViewController : UIViewController {
@IBOutlet var imageView: UIImageView!
var number = 1 {
didSet {
if number == 27 {
number = 1
}
imageView.image = UIImage(named: "card\(number)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "card1")
}
@IBAction func buttonPressed(_ sender: UIButton) {
number = number + 1
}
}
Display Image on a button from a URL in swift
You can use SDWebImage POD.
yourBtn.sd_setImage(with: <#T##URL?#>, for: <#T##UIControl.State#>, completed: <#T##SDExternalCompletionBlock?##SDExternalCompletionBlock?##(UIImage?, Error?, SDImageCacheType, URL?) -> Void#>)
How do I put the image on the right side of the text in a UIButton?
Despite some of the suggested answers being very creative and extremely clever, the simplest solution is as follows:
button.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
As simple as that. As a bonus, the image will be at the left side in right-to-left locales.
EDIT: as the question has been asked a few times, this is iOS 9 +.
Related Topics
Is .Playground a Swift File? Who Can 'See' It
Swift- How to Display Image Over Button
Rotating Uitextview Programmatically
Add a Xib File to a Swift Package
Calling Stop() on Avaudioplayernode After Finished Playing Causes Crash
Fetching Child Sum from Core Data
Swift, Error Exc_Breakpoint (Code=1, Subcode=0X100695474)
Swift 3: Safe Way to Decode Values with Nscoder
Modifying an Array Passed as an Argument to a Function in Swift
How to Make Function That Some Parameters Not Required in When Call It in iOS Swift 3
Swift - Encoding and Decoding String for Special Characters
Swift - How to Resize a Uiview Based on Its Uilabel Size (That Is Inside)
Xcode - Build Setting "Excluded_Source_File_Names" Not Working