Can't Click on UITabelCell after implementing UIRefreshControl - swift

I have recently implemented the RefreshControl on my ViewController which contains UITableDataSource and Delegate. When you click on each cell of the table, it leads to other view controller but after implementing the refresh control, I can no longer click on each cell. Is it because I added the subview(refreshControl) to the view?
Here is the code below,
tableViewController.refreshControl = self.refreshControl
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: Selector("refresh:"), forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
func refresh(sender:AnyObject) {
// Code to refresh table view
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}

Related

Target Action to UIButton of nib not working in parentViewConteroller

I am trying to add targetAction to my UIButton of my subView.
Here is how I add UINib to my parentViewController.
let myView = UINib(nibName: "myNibName", bundle: .main).instantiate(withOwner: nil, options: nil).first as! CarTypeCollectionCell
myView.setData(param, secondParam)
self.mainView.addSubview(myView)
myView.button.addTarget(self, action: #selector(onClickBtn(_:)), for: .touchUpInside)
And here is my onClickBtn function.
#objc func onClickBtn(_ sender: UIButton) {
print("this is clicked")
}
But the button action is not working. I set all of my button and view isUserInteractionEnabled to true.
When I tried with UITapGesture to myNibView, its working pretty well. But not with in that button addTarget case.
Is there something I am missing?

UIRefresh Control UI bug with large titles & collection view

I am using a UICollectionViewController embedded within a UITabBarController & UINavigationController and when I switch between tabs multiple times I am occasionally experiencing a bug where the refresh control appears above the title when I do not want it to.
Refresh Control UI Bug
Debug View Hierachy
var refreshControl: UIRefreshControl = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.primaryRed
return refreshControl
}()
refreshControl.addTarget(self, action: #selector(refreshData), for: .valueChanged)
collectionView?.refreshControl = refreshControl
#objc private func refreshData() {
refreshControl.beginRefreshing()
datasource.fetchEvents {
self.refreshControl.endRefreshing()
self.collectionView?.reloadData()
}
}

swift present controller with a back button to get back to last screen

I am presenting a UICollectionViewController but it just ends up as a controller with no back buttons to get back to the last controller or the home controller, how would i add a back button? or what other options do i have?
class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate, ProfileHeaderClosedDelegate {
func presentFRC() {
let PE = FriendsRequestsController(collectionViewLayout:
UICollectionViewFlowLayout())
let NPE = UINavigationController(rootViewController: PE)
self.present(NPE, animated: true, completion: nil)
}
}
Add the following code in the view did load of the view controller you are presenting.
override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal)
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
self.navigationController?.popViewControllerAnimated(true)
}
You have two options:
Embed your view controllers into UINavigationController and use UINavigationBar and segues for navigation. The easiest way to build such a system would be through storyboard though. But you can do that programmatically as well, but it would be a lot more work.
Add your own back button programmatically the way #ZahurafzalMirza has shown you. Since you are not using UINavigationController, your navigationController will be nil. So the action should look like this
func backAction() -> Void {
self.dismiss(animated: true)
}

Button in uitabbar doesn't push any view controllers swift 3

I've added a button as a subView to tabbarcontroller to open the camera and gallery. It works fine but whenever it needs to push a view controller it does nothing. I removed the function to call the camera and gallery, and it will simply print a text in the console and it works. Meaning the button works fine but when it comes to pushing/presenting VC, nothing..
Does it something to do with the button inserted as a subview to a tab bar? It is supposed to be one of those big buttons in the middle like in Instagram.
I already made the VC to be pushed as a subclass of UITabbarcontroller. Still nothing..
Here's my code:
override func viewDidLoad() {
super.viewDidLoad()
myPickerController.delegate = self
let sell_image = UIImage(named: "Sell icon")
button.setImage(sell_image, for: .normal)
button.setTitle("Sell", for: .normal)
self.view.insertSubview(button, aboveSubview: self.tabBar)
button.addTarget(self, action: #selector(sellVC(sender:)), for: .touchUpInside)
}
then the button function itself:
func sellVC(sender:UIButton!) {
print("this button prints this successfully but doesn't push the VC below")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "dummy") as! DummyViewController
self.navigationController?.pushViewController(vc, animated: true)
}

Swift custom back button in navigation bar

I am trying to use a custom image for my back button in the navigation bar. I am using the below code, which adds the image, but also keeps the text "Back" in the button. I want to also remove the text. Can I do that?
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "icon-back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "icon-back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
Try code below :-)
func SetBackBarButtonCustom()
{
//Back buttion
let btnLeftMenu: UIButton = UIButton()
btnLeftMenu.setImage(UIImage(named: "back_arrow"), for: UIControlState())
btnLeftMenu.addTarget(self, action: #selector(UIViewController.onClcikBack), for: UIControlEvents.touchUpInside)
btnLeftMenu.frame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func onClcikBack()
{
_ = self.navigationController?.popViewController(animated: true)
}
If you want to add Back button in every UIViewController then you can add the code in UIViewController extension else you can use addBackButton() directly as follows.
extension UIViewController {
func addBackButton() {
let btnLeftMenu: UIButton = UIButton()
let image = UIImage(named: "backButtonImage");
btnLeftMenu.setImage(image, for: .normal)
btnLeftMenu.setTitle("Back".localized, for: .normal);
btnLeftMenu.sizeToFit()
btnLeftMenu.addTarget(self, action: #selector (backButtonClick(sender:)), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
}
func backButtonClick(sender : UIButton) {
self.navigationController?.popViewController(animated: true);
}
}
Make sure you should add the following file "backButtonImage.png" in your app bundle.
Call this method self.addBackButton()in your viewDidLoad method of your custom UIViewController class like this
override func viewDidLoad() {
super.viewDidLoad()
self.addBackButton()
}
Note : if you don't add addBackButton Method in extension then you will need to add this method directly in the class and set target and Selector accordingly.