Swift: Button with image bigger than button - swift

I can't figure out a way to make a button look like in the following image.
The icon is supposed to be bigger than the button.
I already tired this, but it doesn't work:
let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode = UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setImage(image, for: UIControlState.normal)

Try this code
let image = UIImage(named: "CircleForButtonCountries")
goToCoutriesButtonMain.imageView?.contentMode =
UIViewContentMode.scaleAspectFit
goToCoutriesButtonMain.setBackgroundImage(image, for: UIControlState.normal) // use setBackgroundImage instead of setImage

Working in Swift 5:
yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)

Related

How can I add two images (one at the left and one on the right side) in a button in Xcode programaticly? I need also a title of a button

func setupButtonUI() {
if let detailsImage = UIImage(named: "detalji_icon") {
setImage(detailsImage, for: .normal)
contentHorizontalAlignment = .left
contentVerticalAlignment = .center
}
if let arrowImage = UIImage(named: "posalji_zahtev_black_icon") {
setImage(arrowImage, for: .normal)
contentHorizontalAlignment = .right
contentVerticalAlignment = .center
}
}/this is one of the things I tried, but whit this I get only the second image and its set in the center not right and the left image is hot even there/
UIButton has only one imageView, you can create custom button like in this answer
https://stackoverflow.com/a/43112735/13628690
Found the next solution: Made a custom UIView and added two images and label as a subview of customView. Did the constraints programaticly. Just added UITapGestureRecognizer (addGestureRecognizer) at the entire view and its all elements are clickable. The design and functionality are satisfied.

How to resize image button on uikit

How to make the image "fill the content" in a UIButton ?
let imageRecord = UIImage(named: icon)?.withTintColor(tintColor, renderingMode: .alwaysOriginal)
self.button.backgroundColor = bgColor
self.button.setImage(imageRecord, for: .normal)
self.button.imageView?.contentMode = .scaleAspectFill
It doesn't work BUT If I change style button from "plain" to "default" that works and i have lot of constraints warning I dont have when I put style = plain ?!

Touch image in navigation bar for redirecting another view

I didnt see the image in navigation bar so how can i give event touch inside for that image ? Need help !
This is my code . Remember the image have not in the view.
Detail more for my issue is notification(the bell)
Try and see if this helps.
let img = UIImage(named: "icon.png");
let btn = UIButton(frame: CGRectMake(0,0,(statImg?.size.width)!,(statImg?.size.height)!));
btn.setBackgroundImage(statImg, forState: .Normal);
btn.addTarget(self, action: #selector(YourController.yourMethod), forControlEvents: .TouchUpInside);
let barBtn: UIBarButtonItem = UIBarButtonItem.init(customView: btn);
navigationItem.rightBarButtonItem = barBtn;

How to get Parallax Effect on UIButton in tvOS?

I'm trying to make some UIButtons in a UIView for a tvOS app but have been unable to get them to display the new parallax effect in the simulator. I successfully set up a TV image stack in images.xcassets called "startReadingButton" and my buttons load with the file, but they do not display the shiny parallax effect when swiping around on the remote in the simulator. Here is how I am loading my UIButtons:
for button in 1...5 {
let image = UIImage(named: "startReadingButton")
let newButton = UIButton(type: .Custom)
newButton.frame = buttonRects[button - 1]
newButton.setBackgroundImage(image, forState: UIControlState.Focused)
newButton.imageView?.image = image
newButton.imageView?.adjustsImageWhenAncestorFocused = true
newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
newButton.tag = button
newButton.canBecomeFocused()
newButton.adjustsImageWhenHighlighted = true
newButton.clipsToBounds = false
self.addSubview(newButton)
self.homeButtons.append(newButton)
}
So far, I have tried almost every variation I could think of to find a solution, like setting the button type to both .Custom and .System, setting the image in different parameters of the UIButton, etc. However I cannot get the buttons to enter parallax mode and move around when they are focused.
Anyone know what I need to do to get the desired parallax effect?
Solved my problem -
Set your LSR or Apple TV image stack in your UIButton's setImage property for the UIControlState.Focused state.
Set the imageView?.clipsToBounds = false on the UIButton.
Set newButton.imageView?.adjustsImageWhenAncestorFocused = true on the UIButton.
I have got the following code to work as expected:
for button in 1...3 {
let buttonUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingUnpressed.png"))!
let buttonPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingPressed.png"))!
let newButton = UIButton(type: .Custom)
newButton.frame = buttonRects[button - 1]
newButton.setImage(buttonUnpressedTexture, forState: UIControlState.Normal)
newButton.setImage(buttonPressedTexture, forState: UIControlState.Highlighted)
newButton.setImage(UIImage(named: "StartReadingButton"), forState: UIControlState.Focused)
newButton.imageView?.clipsToBounds = false
newButton.imageView?.adjustsImageWhenAncestorFocused = true
newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
newButton.tag = button
newButton.canBecomeFocused()
self.addSubview(newButton)
}
"StartReadingButton" is an Apple TV image stack in my images.xcassets catalog.

Referencing a UIButton by tag value

I am aware that a pushed button can be updated by reference to sender.
However I want to access a button from the program body by reference to its tag and then update its label,if that is possible.
Suggestions on how to do this (in Swift) would be appreciated.
To get a button inside a view using its tag, you can use the UIView.viewWithTag function.
if let button = self.view.viewWithTag(tag) as? UIButton
{
button.setTitle("newTitle", forState: UIControlState.Normal)
}
You have to use viewWithTag
ex:
var button = self.view.viewWithTag(tagNumber) as UIButton
button.setTitle("Button Title", forState: UIControlState.Normal)
Set the tag for all buttons. Then have a property
var buttons = [UIButton]
and in your awakeFromNib init it
buttons.append[myButtonWithTag0]
buttons.append[myButtonWithTag1]
// etc
Now you can access your buttons via
let button = buttons[index]
Swift3 : Possible with viewWithTag(button_tag) as:
let tmpButton = self.view.viewWithTag(tag) as? UIButton
tmpButton?.setTitleColor(.red, for: .normal)
tmpButton?.setTitle("Button Title", for: .normal)