I am trying to add multiple buttons to an UIView subclass. In the init() method of the UIView subclass (called leftPanel), I added multiple calls to different functions to add each button. This is how my init() looks like:
override init(frame: CGRect){
super.init(frame: frame)
components = self.setupView()
print("Checkpoint: [leftPanel] init() call finished") // basic debug checkpoint
}
The init() calls a function named setupView() which contains the calls to each of the functions that creates a 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 in the setupView function is to a smaller function which contains the code to add each button to the leftPanel subclass. I have done it this way so I can remove each member as I need, and can use each individual setup function to re-add each individual item onto my view as required. Most of the functions are nearly the same, and uses 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 setup each of the buttons. However, when I run this on the iOS Simulator, the line which is supposed to set the image for each button does not seem to work. When I initialise each button, I initialise them with a type of UIButtonType.infoLight because I want to use the same style of the normal infoLight button (such as the text colour). The only attribute of the infoLight type I would like to change, is the image next to it (which is the (i) by default). I would like to make the icon into an UIImage(contentsOfFile: "up.png"), but it doesn't seem to work. Please can anybody suggest any changes that I could make to achieve this?
I have currently tried to make some changes to what I have, such as changing the type to .custom, seeing if that would change the image, but that does not work either. It only resets the formatting from .infoLight which I'd like to use, so I have switched it back. However, if your solution requires me to change it back to .custom, then I can do it.
I have also tried changing my code to replace up.png with only up but it still doesn't work.
None of my code raises any warnings or errors.
Edit: Most answers have suggested that I change my UIImage(contentsOfFile: "") to UIImage(named: ""), which I have done, but it takes up the entire frame of the UIButton, which I'd like to change, and keep the same as an .infoLight, by keeping a small image with a label on the right. Can anyone suggest a workaround for that too?
What is unique about this question?: Though the .setImage() has been solved by using: UIImage(named:) instead of UIImage(contentsOfFile:), I'd also like to keep the .setTitle() label in the button object. The UIImage(named:) call makes the image fill in the entire frame of the button, but I'd like to keep it the same as .infoLight, with the small image with the title on the right.