How can I change ViewController with tapping an UIImageView? - swift

I created a CollectionView. This view has a userProfileImageView and I want to change view controller to ProfilePage when clicked this user profile image. Try some code but neither present nor push view controller doesn't work to me. Can you help me ?
lazy var userProfileImageView: UIImageView = {
let useriv = UIImageView()
useriv.contentMode = .scaleAspectFill
useriv.clipsToBounds = true
useriv.layer.cornerRadius = 40 / 2
useriv.isUserInteractionEnabled = true
useriv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userImageTap)))
return useriv
}()
#objc func userImageTap() {
print("useriv clicked")
let profilePage = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
}

Related

How do you enable a UIButton in a different view controller?

I have a UIButton in MyViewController called MyButton:
let myButton: UIButton = {
let button = UIButton()
button.isEnabled = false
return button
}()
I can't seem to figure out how to enable this button in another view controller. I tried doing this:
class MyOtherViewController: UIViewController {
func enableButton() {
let upload = MyViewController()
upload.myButton.isEnabled = true
}
}
but the button was not enabled.
It seems super simple, but I can't figure it out... Thanks in advance!

Set navigation item from UINavigationController extension

I wrote a function to show my app logo in the middle of the navigation bar, and it is working fine when I call it from viewDidLoad() function.
But, when I tried to add it to a UINavigationController extension, it is not working! Although the function is called, the logo does not appear.
Any idea?
Thanks
extension UINavigationController {
func showNavLogo(){
let imageW = 59
let imageH = 30
let imageView = UIImageView()
imageView.image = UIImage(named: "Logo.png")
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let currWidth = self.navigationItem.titleView?.widthAnchor.constraint(equalToConstant: CGFloat(imageW))
currWidth?.isActive = true
let currHeight = self.navigationItem.titleView?.heightAnchor.constraint(equalToConstant: CGFloat(imageH))
currHeight?.isActive = true
}
}
I found the solution by adding the function to UIViewController extension instead of UINavigationController
extension UIViewController {
func showNavLogo(){
let imageW = 59
let imageH = 30
let imageView = UIImageView()
imageView.image = UIImage(named: "Logo.png")
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let currWidth = self.navigationItem.titleView?.widthAnchor.constraint(equalToConstant: CGFloat(imageW))
currWidth?.isActive = true
let currHeight = self.navigationItem.titleView?.heightAnchor.constraint(equalToConstant: CGFloat(imageH))
currHeight?.isActive = true
}
Did you remember to call self.showNavLogo() in your viewDidload?
your extension means you have a function called showNavlogo, but you may still have to call the showlogo method by using the self..showNavLogo() in your viewDidload or viewWillAppear.
Also ensure your storyboard or View controller or nib is mapped to a Navigation controller for your viewcontroller to implement your method.
changing the extension to UIViewcontroller worked for me

Cant change colour of searchBar and it jumps to top of view when selected

I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?
Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}

Why is my image not showing up Fullscreen?

I have the following problem. I have about 10 different images which are in a view controller. Now I want the image to switch to fullscreen when they are tapped on. I already set the allow user interaction option to yes, and put in the following code i found on this website:
import UIKit
class ImageViewController: UIViewController {
#IBAction func imageTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Does anyone know why my images are not tappable and turning fullscreen?
Make sure both the image view have userInteractionEnabled = YES and
here's an example for what you are trying to do here https://github.com/abhinavsingh77/ImagePreview

Set textfield in UISearchBar

I'm trying to add SearchController to UINavigationBar. I'm trying to set UITextField of UISearchBar after back button of UINavigationController. I want some more space after back button
When I start searching it appears as
Whereas I should be able to view back button. Only textfield width should be decreased. Also after Cancel it should be again back to initial layout. Whereas it is displayed as below:
Below is my code
var searchResultController = UISearchController()
self.searchResultController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.delegate = self
controller.hidesNavigationBarDuringPresentation = false
self.navigationController?.navigationBar.addSubview(controller.searchBar)
return controller
})()
override func viewDidAppear(animated: Bool) {
for subView in searchResultController.searchBar.subviews{
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
var bounds: CGRect
bounds = textField.frame
bounds.size.width = self.view.bounds.width - 50
}
}
}
}
Please let me know how can I fix this.
Thanks in advance
For setting the UIsearchBar add it to Navigation's titleView as
self.navigationItem.titleView = controller.searchBar
For removing Cancel button we can use UISearchControllerDelegate method
func didPresentSearchController(searchController: UISearchController) {
searchController.searchBar.showsCancelButton = false
}
Hope this may help any one.