Setting an image as Navigation Bar title - ios

I'm trying to set an image as the title of a navigation bar in swift using the code below. The code builds successfully, but the image does not display. Any advice would be appreciated.
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(contentsOfFile: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.navigationBar.titleView = self.convoImageView
}
}

Set the navigationItem's titleView.
let image = UIImage(named: "image_name.png")
self.navigationItem.titleView = UIImageView(image: image)
In your code replace below line:
self.convoImage = UIImage(contentsOfFile: "conversation.png")
with
self.convoImage = UIImage(named: "conversation.png")

You should set convoImageView frame .
import UIKit
class FirstViewController: UIViewController {
#IBOutlet weak var navigationBar: UINavigationItem!
var convoImage: UIImage!
var convoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.convoImage = UIImage(named: "conversation.png")
self.convoImageView = UIImageView(image: self.convoImage)
self.convoImageView.frame = CGRectMake(0,0,720,100)
self.navigationBar.titleView = self.convoImageView
}
}

Related

How can I add navigation bar with an image instead of a title in a UIViewController?

Currently this is what I have:
override func viewDidLoad() {
super.viewDidLoad()
self.setNavigationBar()
}
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 44))
let navItem = UINavigationItem(title: "")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: nil, action: #selector(done))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
#objc func done() { // remove #objc for Swift 3
The issue is that the bars title seems to be off center and the bar is very thin.
How can it be made a little thicker and with an image instead of the title?
UPDATE:
The first suggestion seemed to do nothing. Why is that? This is the first view controller.
Full code:
import UIKit
import Firebase
import FirebaseAuth
class LoginViewController: UIViewController {
#IBOutlet weak var emailField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var gifView: UIImageView!
private var ref: DatabaseReference!
override func viewDidLoad() {
let logo = UIImage(named: "q-small.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
gifView.loadGif(name: "truck-animation")
super.viewDidLoad()
ref = Database.database().reference()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
This is similar to Swift Navigation Bar Image Title.
Simply use this code:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView
It seems like you are creating UINavigationBar programmatically and i am also assuming that you are creating app using storyboard.
self.navigationItem returns navigation items of your ViewController. But as per your code you must have created just UINavigationBar. So to get the title view you need UINavigationController of your ViewController.
If this is your first view of app You can add that by embedding your ViewController into Navigation Controller from Storyboard, then you don't have to add navigation bar programmatically.
After that you can use this code in viewDidLoad().
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView

How to get WKWebView to show in background

I am trying to get an animated gif to play in the background, and have some labels and buttons appear on top. This code runs and shows the animated gif, however it does not show the buttons or label text.
import Foundation
import UIKit
import WebKit
class LoginViewController: UIViewController {
#IBOutlet var containerView: UIView! = nil
var webViewBG: WKWebView?
#IBOutlet weak var loginButton: UIButton!
#IBOutlet weak var signUpButton: UIButton!
override func loadView() {
super.loadView()
self.webViewBG = WKWebView()
self.view = self.webViewBG
}
override func viewDidLoad() {
super.viewDidLoad()
let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
let htmlURL = URL(fileURLWithPath: htmlPath!)
let html = try? Data(contentsOf: htmlURL)
var req = NSURLRequest(url:htmlURL)
self.webViewBG!.load(req as URLRequest)
webViewBG?.isUserInteractionEnabled = false;
self.loginButton.layer.borderColor = UIColor.white.cgColor
self.loginButton.layer.borderWidth = 2
self.signUpButton.layer.borderColor = UIColor.white.cgColor
self.signUpButton.layer.borderWidth = 2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
It looks like this line overrides your view controller's view where the buttons are:
self.view = self.webViewBG
Try
self.view.addSubview(self.webViewBG)
You might also need to tweak the z position so that the webView is located 'behind' the buttons, like this:
self.webViewBG.layer.zPosition = 0.5
Higher zPosition means the view stands out more 'towards' the user

Set Title to UIBarButtonItem

I have UIBarButtonItem and whenever I try to set title to the button, its not working.
#IBOutlet weak var barButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
barButton.title = "AB"
}
But if I try to create Action of UIBarButtonItem and then do sender.setTitle() then it works. But not at viewDidLoad. I don't want to set title when user taps on the button.
Change title or image and then resign it back to navigationItem:
class AnotherViewController: UIViewController {
#IBOutlet weak var myBarButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let barButton = myBarButton
barButton?.title = "Anther Title"
navigationItem.rightBarButtonItem = barButton
}
}
Apple document: https://developer.apple.com/reference/uikit/uibaritem/1616412-title
You should set this property before adding the item to a bar. The default value is nil.
If you already have an IBOutlet for the barButton, why not just set the title in the storyboard? I think this is the easiest way unless you have some other reason for doing it programmatically.
When I try this, it works. Maybe there is a bug with Xcode, delete your button and connections then try again.
class ViewController: UIViewController {
#IBOutlet weak var barButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
barButton.title = "MyButton"
}
Try this instead
#IBOutlet weak var barButton: UIBarButtonItem! {
didSet {
barButton.title = "AB"
}
}

Positioning images and that are unhidden

I'm working on a project to have hidden images appear when the button is pressed. My only problem is that when I click on the button the image appears. I then have to click on the button again for it to show up in the correct area of my view controller. Can someone help point me in the right direction please?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.hidden = true
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.hidden = false
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
}
This may or may not help, but i would recommend that instead of using the image.hidden property, set the alpha value of the image. So image.alpha = 0 for hidden and image.alpha = 1 for visible. This would also allow you to then fade the image in and out if you wanted. Additionally I would set the image in viewDidLoad() instead of the Action. Here's how I would redo your code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.image.alpha = 0
image.image = UIImage(named: "diamond.png")
image.frame = CGRectMake(50, 75, 30, 35)
}
#IBAction func test(sender: AnyObject) {
self.image.alpha = 1
}
}
I also believe setting the frame twice isn't useful as the frame will be set in viewDidLoad()

How Do I Add UIViewController to UIScrollView in Swift

I want to have a UIScrollView of UIViewControllers. I tested the ScrollView and it works fine, except when I try to add the ViewControllers. The project builds fine, but I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" error that points at the ViewController, particularly the line:
self.imageView.image = UIImage(named: self.image!)
However, when I inspect the object, I can see variables image and text have values in the ViewController. I'm using Xcode 6.3 Beta and building to iOS8.1 target.
Here's the code:
import UIKit
class ViewController: UIViewController {
//#IBOutlet weak var contentScrollView: UIScrollView!
#IBOutlet var contentScrollView: UIScrollView!
//test to make sure ScrollView functions
//let colors = [UIColor.redColor(),UIColor.blueColor(), UIColor.greenColor(), UIColor.whiteColor(), UIColor.blackColor(), UIColor.yellowColor()]
var frame = CGRectMake(0, 0, 0, 0)
var dataArray = [PostItem]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var da = PostItem(text: "test 1", pic: "beans.jpg")
self.dataArray.append(da!)
da = PostItem(text: "test 2", pic: "coffee.jpg")
self.dataArray.append(da!)
da = PostItem(text: "test 3", pic: "coffeeCup.jpg")
self.dataArray.append(da!)
for index in 0..<self.dataArray.count{
self.frame.origin.y = self.contentScrollView.frame.size.height * CGFloat(index)
self.frame.size = self.contentScrollView.frame.size
self.contentScrollView.pagingEnabled = false
let tempPost = self.dataArray[index]
var vc = PostViewController()
vc.text = tempPost.postText
vc.image = tempPost.postImage
self.contentScrollView.addSubview(vc.view)
}
self.contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGFloat(self.dataArray.count) * self.contentScrollView.frame.height)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}}
And here is the code for the ViewController I want to add:
import UIKit
class PostViewController: UIViewController {
//let postItem
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var postToFBButton: UIButton!
#IBOutlet weak var postToTwitterButton: UIButton!
#IBOutlet weak var postText: UILabel!
var image:String?
var text:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.imageView.image = UIImage(named: self.image!)
self.postText.text = text!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}}
The fix is to create the PostViewController through instantiateViewControllerWithIdentifier from storyboard, this way, it will have an imageView loaded from storyboard when its view is added to the scrollview.

Resources