0

So I am working on a project building a 3rd party keyboard app that lets the user press a single button and will input a line of text. Very simple. Everything works fine, all my code is working ect. except I cannot figure out how to have my button be represented by an image and not by text.

As you can see below, everything looks good in the interface builder and in my code:

enter image description here

Main code in KeyboardViewController.swift:

import UIKit

class KeyboardViewController: UIInputViewController {

var keyboardView: UIView!

@IBOutlet var nextKeyboardButton: UIButton!

override func updateViewConstraints() {
    super.updateViewConstraints()
    // Add custom view sizing constraints here
}

override func viewDidLoad() {

    super.viewDidLoad()


    self.loadInterface()

}

func loadInterface() {

    var keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)

    self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as! UIView

    view.addSubview(self.keyboardView)

    view.backgroundColor = self.keyboardView.backgroundColor

    self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)

}

But when I run my app in the simulator, all I see is a text button and not the image that I had provided.

enter image description here

What is going on, and what can I do to fix this seemingly simple problem? I'm stumped.

Ahad Sheriff
  • 1,829
  • 8
  • 24
  • 46

4 Answers4

1

I don't know how to mark this question as a duplicate, but the answer to your question is here. Depending on what you want the button to behave like you'll have to get your hands dirty and assign the image manually using code.

UIButton: set image for selected-highlighted state

Create a button programmatically and set a background image

Community
  • 1
  • 1
Zach Morris
  • 367
  • 1
  • 4
  • 12
  • I'll have to assign the image manually, even if I want the image to behave as the default button does? Because I literally just want to display the image rather than text... – Ahad Sheriff Jul 14 '15 at 03:00
  • And if I were to programmatically set the background image, then how would I completely replace everything I did in the interface builder? – Ahad Sheriff Jul 14 '15 at 03:11
  • your added code should override what's happening in interface builder. Otherwise when you want to change the text of the a label, your default text would override what your code is doing. It should take you about 5 minutes to try out one of the answers given in those posts. – Zach Morris Jul 15 '15 at 06:19
1

When you are trying displaying image in a button

1.Try deleting title and leave empty like this

2.Make sure that your image exist and all the view are connected to each other.

Or try this tutorial,hope this help....

Tutorial Custom Keyboard

Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
0

try using this,

button.setImage(UIImage(named: "selected"), forState: UIControlState.Selected.union(UIControlState.Highlighted))
Sachin Nautiyal
  • 568
  • 4
  • 15
  • http://stackoverflow.com/help/how-to-answer: "Brevity is acceptable, but fuller explanations are better." Thus the knowledge base which SO is will really help the future readers. – Honza Zidek Nov 27 '15 at 09:07
0

try using this

self.Button.setBackgroundImage(image: (insert image name here), forState: UIControlState.Normal)