UIButton.setImage () not working in Swift 3
I am trying to add multiple buttons to a subclass UIView
. In a init()
subclass method UIView
(called leftPanel
), I added several calls to various functions to add each button. This is what mine looks like init()
:
override init(frame: CGRect){
super.init(frame: frame)
components = self.setupView()
print("Checkpoint: [leftPanel] init() call finished") // basic debug checkpoint
}
init()
calls a named function setupView()
that contains calls to each of the functions that create the button. Here is my code for setupView()
:
func setupView() -> [UIView] {
let addButton = self.setupAddButton()
let infoButton = self.setupInfoButton()
let orderPromoteButton = self.setupOrderPromoteButton()
let orderDemoteButton = self.setupOrderDemoteButton()
let refreshItemsButton = self.setupRefreshItemsButton()
let components = [lessonActionsLabel, addButton, infoButton, orderPromoteButton, orderDemoteButton, refreshItemsButton]
return components
}
And each of the calls to the function setupView
has a smaller function that contains the code to add each button to the subclass leftPanel
. I did it in such a way that I can remove each member as I need and can use each individual customization function to re-add each individual item to my view as needed. Most of the functions are almost the same and use this code:
func setupRefreshItemsButton() -> UIButton {
let refreshItemsButton = UIButton(type: UIButtonType.infoLight)
refreshItemsButton.frame = CGRect(x:0, y:screenHeight*55/100, width: screenWidth*30/100, height: screenHeight*5/100)
refreshItemsButton.setTitle(" Refresh lesson items", for: UIControlState.normal)
refreshItemsButton.setImage(UIImage(contentsOfFile: "up.png"), for: UIControlState.normal)
self.addSubview(refreshItemsButton)
return refreshItemsButton
}
to customize each of the buttons. However, when I run this on the iOS Simulator, the line that should set the image for each button doesn't seem to work. When I initialize each button, I initialize them with a type UIButtonType.infoLight
because I want to use the same style of a normal button infoLight
(like text color). The only type attribute infoLight
I would like to change is the image next to it (which is (i) by default). I would like to make an icon in UIImage(contentsOfFile: "up.png")
, but it doesn't work. Can anyone suggest any changes I could make to achieve this?
I am currently trying to make some changes to what I have, for example change the type to .custom
if that changes the image, but that won't work either. It only resets the formatting from .infoLight
which I would like to use, so I switched it back. However, if your decision requires me to change it to .custom
, then I can do so.
I also tried changing my code to replace up.png
only up
, but it still doesn't work.
None of my codes throw any warnings or errors.
Edit . Most of the answers suggested that I change mine UIImage(contentsOfFile: "")
to UIImage(named: "")
, which I did, but it takes up the entire UIButton frame that I would like to change and keep the same as .infoLight
keeping a small image with a label on the right. Can anyone suggest a workaround for this?
What's unique on this matter ?: Although .setImage () was solved with: UIImage(named:)
instead UIImage(contentsOfFile:)
, I would also like to store the .setTitle()
label in the button object. The call UIImage(named:)
makes the image fill the entire frame of the button, but I would like to keep it the same .infoLight
, with a small image with a title on the right.
source to share
After some research, I found out more about UIButton
in particular. It inherits from UIView
, which means you can use .addSubview()
on UIButton
like on UIView
.
For example, this can be done:
let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let myButtonBackgroundImage = UIImage(named: "icon.png")
let myButtonBackgroundImageView = UIImageView(frame: x: 0, y: 0, width: 20, height: 20)
myButtonBackgroundImageView.image = myButtonBackgroundImage
myButton.addSubview(myButtonBackgroundImageView)
let myButtonTitleLabel = UILabel(x: 5, y:5, width: 10, height: 10)
myButtonTitleLabel.text = "Hi"
myButton.addSubview(myButtonTitleLabel)
So, essentially, although there is no direct method, handling UIButton
like a UIView
could create a workaround like the one above that displays with a background image and a title in front of it, which is exactly what I was looking for.
source to share
Use this code -
func setupRefreshItemsButton() -> UIButton {
let refreshItemsButton = UIButton(type: UIButtonType.custom)
refreshItemsButton.frame = CGRect(x:0, y:screenHeight*55/100, width: screenWidth*30/100, height: screenHeight*5/100)
refreshItemsButton.setTitle(" Refresh lesson items", for: UIControlState.normal)
refreshItemsButton.setImage(UIImage(named: "up.png"), for: UIControlState.normal)
self.addSubview(refreshItemsButton)
return refreshItemsButton
}
source to share
If you need an image to the left of the button title try this: -
refreshItemsButton.setImage(UIImage(named: "up.png"), for: .normal)
refreshItemsButton.imageEdgeInsets = UIEdgeInsetsMake(0,-10, 0, 0)
refreshItemsButton.imageView?.layer.masksToBounds = true
refreshItemsButton.contentMode = .scaleAspectFit
Hope it helps.
source to share