Annotation with Custom Callout icon - ios

I'm trying to show a custom icon on rightCalloutAccessoryView
I tried the codes below but it didn't work.
let iconButton = UIButton(type: .system)
iconButton.setImage(UIImage(named: "test.png"), for: .normal)
pinView?.rightCalloutAccessoryView = iconButton
Is it possible to add a custom png icon to Callout button ?
Edit : When I changed the button type with .detailDisclosure it worked.
let iconButton = UIButton(type: .detailDisclosure)
iconButton.setImage(UIImage(named: "test.png"), for: .normal)
pinView?.rightCalloutAccessoryView = iconButton

Related

Swift: navigation bar item text and image

I use this code to show image and text in navigation bar item.
func barItem() {
let button = UIButton(type: .system)
button.setImage(UIImage(named: "trashButtonItem.png"), for: .normal)
button.setTitle("Читать", for: .normal)
button.sizeToFit()
self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: button), animated: true);
}
In this case I have image on the left and text on the right. But I want to have image on the right and text on the left. How to do it?
You can change the button semanticContentAttribute
func barItem() {
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "trashButtonItem.png"), for: .normal)
button.setTitle("Читать", for: .normal)
button.semanticContentAttribute = .forceLeftToRight
button.sizeToFit()
self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: button), animated: true);
}
You can also handle RTL
button.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
You need to adjust the titleEdgeInsets and the imageEdgeInsets of your UIButton.
Before:
After:

Enable tint color change animation when creating UIButton programmatically

I'm not using the Storyboard to create my UI so I've created my button like so
var randomButton: UIButton = {
var button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Random", for: .normal)
button.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
button.layer.cornerRadius = 3
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.setTitleColor(.black, for: .highlighted)
button.addTarget(self, action: #selector(HomeController.buttonPressed), for: .touchUpInside)
return button
}()
The issue is that when the user taps the button it doesn't have a nice transition/animation between the tint colors. The color of the text doesn't change to black unless the user holds the button.
The normal behavior, when you use the Storyboard, the blue tinted text changes to a light blue color slowly. Not abruptly.
I think the animation you are referring to is part of the system type button.
Try this:
var button = UIButton(type: .system)

Custom button as leftBarButtonItem

I'm using this code to display a custom button as leftButtonItem:
let button = UIButton(type: .system)
button.setTitleColor(.white, for: .normal)
button.setImage(UIImage(named: "Back"), for: UIControlState.normal)
button.addTarget(self, action: #selector (DetailExperienceTVC.back), for: .touchUpInside)
button.setTitle(DataManager.shared.arrayMenuTop[DataManager.shared.indexTitle].title, for: .normal)
button.titleLabel?.font = ColorManager.shared.generalFont30
button.sizeToFit()
button.titleEdgeInsets = UIEdgeInsetsMake((self.navigationController!.navigationBar.frame.height) / 4, 10, 0, 0)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
but this is what I get wrong size button, my question is how can I distanziate the text from the image without cropping it?
Thanks in advance!
Here's the fix:
button.titleLabel?.adjustsFontSizeToFitWidth = true

Change UIButton textlabel color by touching the button Swift 3

I read some articles but didn't find a solution that solved my problem. Currently im building an iOS app in Swift 3. I placed a UIButton called registerButton into my view. If someone touches the button a new ViewController shall appear(newController). Beside that the color of the registerButton.textlabel.setColor() should be changed when a user touches the button and hold it pressed ? The presentation of the newController works but not the change color of the textLabel. Im changing the color of the registerButton in the Registration function. Has someone an idea ? Thanks
var registerButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Registration", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(registration), for: .touchUpInside) // Action
return button
}()
func registration(){
let color = UIColor(r: 255, g: 0, b: 102)// red
registerButton.setTitleColor(color, for: .highlighted)
let newController = ViewController()
present(newController, animated: true, completion: nil)
}
Button normal
Button pressed no red text label
var registerButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Registration", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(colorChange), for: .touchDown)
button.setTitleColor(.red, for: .highlighted)
// Action
return button
}()
This should work just fine
Try to change the color for the state Focused when you are creating the button, not when pressed.
var registerButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Registration", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.setTitleColor(UIColor.red, for: .focused)
button.addTarget(self, action: #selector(registration), for: .touchUpInside) // Action
return button
}()

UIButton selected image not working

I have a problem with the UIButton, when it's selected. The code is in Swift 3:
let radioButton = UIButton(type: .system)
radioButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal)
radioButton.setImage(UIImage(named: "buttonChecked"), for: .selected)
radioButton.setTitle("This is some text.", for: .normal)
radioButton.sizeToFit()
When it's selected by radioButton.isSelected = true, no image is shown.
You could try to add a target like this
radioButton.addTarget(self, action: "addMethodName", forControlEvents: UIControlEvents.TouchUpInside)
and in the method you change the image like this :
radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal)
You should probably use Custom button type instead of System type. Try this:
let radioButton = UIButton(type: .Custom)
instead of
let radioButton = UIButton(type: .system)

Resources