I understand there are other questions like this but so far I have not been able to get an answer that works for so this is why I am writing this.
I right now have the following code to place a leftBarButtonItem:
let user = FIRAuth.auth()?.currentUser
var imageView: UIImageView?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupNavBar()
}
func setupNavBar() {
//left bar button item setup
let url = URL(string: (self.user?.photoURL?.absoluteString)!)
let data = try? Data(contentsOf: url!)
imageView?.image = UIImage(data: data!)
imageView?.layer.masksToBounds = true
imageView?.layer.borderWidth = 1.5
imageView?.layer.borderColor = UIColor.white.cgColor
imageView?.layer.cornerRadius=(imageView?.bounds.width)! / 2
let profileImageButton = UIBarButtonItem(image: UIImage(named: (self.user?.photoURL?.absoluteString)!), style: .plain, target: self, action:#selector(UserGroupsMainViewController.navLeftBarButtonTapped))
self.navigationController?.navigationItem.leftBarButtonItem = profileImageButton
}
func navLeftBarButtonTapped() {
}
For some reason, the button does not appear on the navigation controller. I do know for sure that I have a URL that is not nil because I tested it in my print output. Any help would be appreciated. Thanks!
Just use navigationItem not navigationController.navigationItem
Ok, I just saw your problem.
You are using a URL as the "name" of an image. It doesn't work that way.
The function UIImage(named: "blah") will load an image from the app bundle stored with the name given.
If you want to download the image from a URL then you need to have a download task run in the background and then load the image once that's done.
Try using the AlamofireImage framework also.
Related
I am getting a delay on animation when popping a view controller.
The reason I am getting the delay is because I have a background image.
override func viewDidLoad() {
super.viewDidLoad()
setBackgroundImage()
}
#IBAction func navigationButton(_ sender: Any) {
print("navigation button presssed")
self.navigationController?.popViewController(animated: true)
}
func setBackgroundImage() {
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "RewardsBackgroundONLY")
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
How do I prevent the delay from happening while setting the background image.
I also tried popping the view controller on the main thread, still doesnt work.
Probably your image is too large and the content mode backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill cause this behavior.
you have two ways to resolve this problem.
change the UIView.ContentMode value and check if is ok for you. try scaleToFill or scaleAspectFit
resize image to fit on your app.
just set
clipsToBounds = true
it works for me
How can I implement the fullscreen image and dismiss image functionality of the Apple Photos? Additionally, how can I get the aspect ratio of an image to fit within a fullscreen view? Are they sending a UIImage to a new view controller?
My current method of fullscreening an image simply sets a UIImageView's frame equal to the superview's frame while turning the alphas of the UINavigationBar and UITabBar to 0. And to dismiss, I added a tap gesture recognizer that reverses the alphas and removes the UIImageView from the superview.
Here's my fullscreen and dismiss code
func fullscreen(forImage image: UIImage) {
let imageView = UIImageView(image: image)
imageView.frame = self.view.frame
imageView.backgroundColor = .black
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.isUserInteractionEnabled = true
self.navigationController?.navigationBar.alpha = 0
self.tabBarController?.tabBar.alpha = 0
let dismissTap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
imageView.addGestureRecognizer(dismissTap)
self.view.addSubview(imageView)
}
#objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
self.navigationController?.navigationBar.alpha = 1
self.tabBarController?.tabBar.alpha = 1
}
This could easily be achieved using Apple's QuickLook framework as it works great for many extensions and collections of images.
Edit: Most of the functionality you want is built into the QLPreviewController
let previewController = QLPreviewController()
previewController.dataSource = self
self.present(previewController, animated: true, completion: nil)
The data source is whatever class conforms to the QLPreviewControllerDataSource protocol
Here is a video guide from apple on how to achieve this easily
Edit: This part goes in the previewItemAt function
guard let url = Bundle.main.url(forResource: "imageName", withExtension: "jpg")
else {
fatalError("Could not load imageName.jpg")
}
return url as QLPreviewItem
Question
Using breakpoints, I found that filterBarButton is not nil, but the filterBarButton?.value(forKey: "view")is nil. The filterBarButton should have a view because I assigned it to a button in the viewDidLoad. Why is this happening? How do I fix this?
I use the frame of right bar button (the Filter button) to place the triangle image underneath it as shown below. I want the triangle image to be centered and directly underneath the Filter button.
Code
override func viewDidLoad() {
let filterButton = UIBarButtonItem(title: "Filter", style: .plain, target: self, action: #selector(filterButtonTapped))
navigationController?.navigationBar.topItem?.rightBarButtonItem = filterButton
setUpFilter()
}
func setUpFilter() {
let filterBarButton = navigationController?.navigationBar.topItem?.rightBarButtonItem
let btnView = filterBarButton?.value(forKey: "view") as AnyObject
}
Reference
I'm using CarbonTabSwipeNavigation in my code (as follows). The issue is that the colour of all the icons is being updated automatically (probably to some default colour). How to fix that?
Apart from that, one of my icons is being updated (don't know why). I tried using the same icon in other parts of the application and it worked perfectly fine. Please help me in this. Thanks
weak var tab: CarbonTabSwipeNavigation!
override func viewDidLoad() {
super.viewDidLoad()
let iconNames = ["icn_events", "icn_places", "icn_activities", "icn_clubs"]
var images = [UIImage]()
for icon in iconNames {
if let img = UIImage(named: icon) {
images.append(img)
}
}
let carbonTabSwipeNavigation = CarbonTabSwipeNavigation(items: images, delegate: self)
carbonTabSwipeNavigation.pagesScrollView?.isScrollEnabled = false
carbonTabSwipeNavigation.insert(intoRootViewController: self)
//I'm using following piece of code to fix the color issue (please note, even without this, the image is being updated).
//carbonTabSwipeNavigation.setNormalColor(UIColor(hex: "5603ad"))
//carbonTabSwipeNavigation.setSelectedColor(UIColor(hex: "5603ad"))
carbonTabSwipeNavigation.setTabExtraWidth(45)
tab = carbonTabSwipeNavigation
}
I've attached the actual icon and the screenshot of how it's being displayed.
It was happening because of bar tint color. The issue was fixed the issue by image rendering mode:
let iconNames = ["icn_events", "icn_places", "icn_activities"]
var images = [UIImage]()
for icon in iconNames {
if let img = UIImage(named: icon) {
images.append(img)
}
}
if let img = UIImage(named: "icn_clubs")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) {
images.append(img)
}
How Do I add multipleImages for the top part and have it scrollable without moving the rest of the content? Also, how do I have the little dots at the bottom to indicate which image I'm on? My code for the image is down below.
import SDWebImage
#IBOutlet weak var headerImage: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let imageUrl:NSURL? = NSURL(string: headerPhoto!)
if let url = imageUrl {
headerImage.sd_setImage(with: url as URL!)
}
}
You are looking for something called image slide show or image slider.
Instead of creating them by yourself which requires lots of effort, here is a GitHub library that is easy to use.
Basicly the way slider works is that it is a horizontal scroll view and each cell in the scroll view is an image so that you can scroll. Then, put a fixed page index element on the bottom of the scroll view to tell you which image you are currently at.
To use it, create a UIView in your viewcontroller and set both class and module to ImageSlideshow. Then connect it to your ViewController.swift as IBOutLet.
Then create an array of image urls
let alamofireSource = [AlamofireSource(urlString: "imgurl1")!, AlamofireSource(urlString: "imgurl2")!, AlamofireSource(urlString: "imgurl3")!]
And finally in your viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
slideshow.backgroundColor = UIColor.white
slideshow.slideshowInterval = 5.0
slideshow.pageControlPosition = PageControlPosition.underScrollView
slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
slideshow.pageControl.pageIndicatorTintColor = UIColor.black
slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill
slideshow.setImageInputs(alamofireSource)
}