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)
Related
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
I want to set image and title both on rightBarButton. Title will come first. I searched a lot but all the articles are related to leftBarButton. So can anyone please tell me, how to set both of them?
func methodForSettingButtonClicked() {
let buttonSetting = UIButton(type: .custom)
buttonSetting.frame = CGRect(x: 0, y: 5, width: 25, height: 25)
let imageReg = UIImage(named: "setting")
buttonSetting.setTitle("Settings", for: .normal)
buttonSetting.setImage(imageReg, for: .normal)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = buttonSetting
self.navigationItem.rightBarButtonItem = leftBarButton;
}
try barbutton item with custom VIew as follows:-
let button = UIButton(type: .Custom)
button.setImage(UIImage(named: "icon_right"), forState: .Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
button.frame = CGRectMake(0, 0, 53, 31)
button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32)//move image to the right
let label = UILabel(frame: CGRectMake(3, 5, 50, 20))
label.font = UIFont(name: "Arial-BoldMT", size: 16)
label.text = "title"
label.textAlignment = .Center
label.textColor = UIColor.whiteColor()
label.backgroundColor = UIColor.clearColor()
button.addSubview(label)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
imageEdgeInsets plays a important role.
Try it as below and set title as you want
let button = UIButton(type: .system)
button.setImage(UIImage(named: "icon_image_name"), for: .normal)
button.setTitle("Categories", for: .normal)
button.addTarget(self, action: #selector(clickOnButtonActionMethod), for: .touchUpInside)
button.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
If you want then change button property as you want.
You can do like this way,
let button = UIButton(type: .custom)
button.setTitle("Settings", for: .normal)
button.setImage(#imageLiteral(resourceName: "settings"), for: .normal)
button.semanticContentAttribute = .forceRightToLeft
button.setTitleColor(UIColor.white, for: .normal)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
It works for me, hope it can help you.
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
}()
In my app, I would like to add multiple images on a single UIButton.
This is what I have now:
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
view.addSubview(NextButton)
I would like a 2nd image as an arrow pointing to the next page.
thanks in advance :)
the rest of the button works but I don't know how to make the arrow appear
I fixed the problem here is the new code:
let screenSize: CGRect = UIScreen.main.bounds
let width = screenSize.width
let height = screenSize.height
NextButton = UIButton(frame: CGRect(x: width*0, y: height*0.55, width: width*0.95, height: height*0.05))
NextButton.addTarget(self, action: #selector(OnNextPressed), for: .touchUpInside)
NextButton.isExclusiveTouch = true
NextButton.setTitle("ButtonText", for: UIControlState())
NextButton.setTitleColor(UIColor(red:0, green:0, blue:0, alpha:1.0), for: UIControlState())
NextButton.setBackgroundImage(UIImage(named: "ButtonOutline"), for: .normal)
NextButton.setImage(UIImage(named: "arrow_next"), for: UIControlState())
NextButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, width*0.7)
view.addSubview(NextButton)
by simply adding setImage to the code i could create a second image on top of the BackgroundImage. then by suing imageEdgeInsets i aligned the image on the button.
In my app I have a flow which goes like: The user press a button which changes the UIBarButtonItems and when the user clicks on one of them, its changes to something else. However I get the following exception when click on one of them.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App.MapViewController revealLeftView]: unrecognized selector sent to instance 0x7fbc90c404c0'
First the buttons added in the storyboard gets their action set like this:
menuButton.action = #selector(PBRevealViewController.revealLeftView)
toolboxMenuButton.action = #selector(PBRevealViewController.revealRightView)
When the user clicks on a button elsewhere on the screen, I change the UIBarButtons like this:
let leftButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
leftButton.setImage(UIImage(named: "close"), for: UIControlState.normal)
//add function for button
leftButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
//set frame
leftButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let leftBarButton = UIBarButtonItem(customView: leftButton)
let rightButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
rightButton.setImage(UIImage(named: "add"), for: UIControlState.normal)
//add function for button
rightButton.addTarget(self, action: #selector(MapViewController.confirmCreateFields(sender:)), for: UIControlEvents.touchUpInside)
//set frame
rightButton.frame = CGRect(x: 0, y: 0, width: 24, height: 20)
let rightBarButton = UIBarButtonItem(customView: rightButton)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = rightBarButton
self.navigationItem.leftBarButtonItem = leftBarButton
This works as it should, and when the user clicks the close-button, I change the UIBarButtonItems like this:
let leftButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
leftButton.setImage(UIImage(named: "MenuIcon_1"), for: UIControlState.normal)
//add function for button
//leftButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
//set frame
leftButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
let rightButton: UIButton = UIButton(type: UIButtonType.custom)
//set image for button
rightButton.setImage(UIImage(named: "ToolsIcon_1"), for: UIControlState.normal)
//add function for button
//rightButton.addTarget(self, action: #selector(MapViewController.cancelAddFields), for: UIControlEvents.touchUpInside)
//set frame
rightButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
leftButton.addTarget(self, action: #selector(PBRevealViewController.revealLeftView), for: UIControlEvents.touchUpInside)
rightButton.addTarget(self, action: #selector(PBRevealViewController.revealRightView), for: UIControlEvents.touchUpInside)
let leftBarButton = UIBarButtonItem(customView: leftButton)
let rightBarButton = UIBarButtonItem(customView: rightButton)
//assign button to navigationbar
self.navigationItem.rightBarButtonItem = rightBarButton
self.navigationItem.leftBarButtonItem = leftBarButton
It shows all up good, but when I click on one of them it throws the exception mentioned above. What am I doing wrong? I've tried the .action on the UIBarButtonItem itself, doesn't seem to work either.
It seems that in:
leftButton.addTarget(self, action: #selector(PBRevealViewController.revealLeftView), for: UIControlEvents.touchUpInside)
self (the target) is not actually an instance of PBRevealViewController but a MapViewController. That means that you are telling the button to call method revealLeftView on a MapViewController.
Make sure you are using the correct target.