Thursday 29 June 2017

UIScrollView's origin changes after popping back to the UIViewController


override func viewWillAppear(_ animated: Bool) {
        
        self.Scrollview.contentOffset = CGPoint(x: 0,y :0)

    }

Wednesday 28 June 2017

how to Scroll(horizontally) in collection view on button press?

https://stackoverflow.com/questions/34024031/how-to-scrollhorizontally-in-collection-view-on-button-press

How to Apply Gradient to background view of iOS Swift App

https://stackoverflow.com/questions/24380535/how-to-apply-gradient-to-background-view-of-ios-swift-app

Saturday 24 June 2017

Create UIWebView Programmatically and Load Webpage Using NSURL

  1. class ViewController: UIViewController, UIWebViewDelegate {
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. // Do any additional setup after loading the view, typically from a nib.
  5. let myWebView:UIWebView = UIWebView(frame: CGRect(x:0, y:0, width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
  6. self.view.addSubview(myWebView)
  7. myWebView.delegate = self
  8. //1. Load web site into my web view
  9. let myURL = URL(string: "http://www.swiftdeveloperblog.com")
  10. let myURLRequest:URLRequest = URLRequest(url: myURL!)
  11. myWebView.loadRequest(myURLRequest)
  12. }

How to check if a file exists in Documents folder?

let documentsURL = try! FileManager().url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true) let fooURL = documentsURL.appendingPathComponent("foo.html") let fileExists = FileManager().fileExists(atPath: fooURL.path) if fileExists==true { print("already existed") } else { print("no file") }

https://stackoverflow.com/questions/1638834/how-to-check-if-a-file-exists-in-documents-folder

Friday 23 June 2017

Enable zooming/pinch on UIWebView

1)you can use webView.scalesPageToFit=YES; programmatically


2)If you are using in xib than just click the check box "Scaling" scales Page to fit

Wednesday 21 June 2017

Go to rootView when tabBar tapped

import UIKit

class RootviewTabViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let rootView = self.viewControllers![self.selectedIndex] as! UINavigationController
        rootView.popToRootViewController(animated: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

  
}

Tuesday 20 June 2017

How to dismiss number keyboard after tapping outside of the textfield

viewdidload()
{
let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(ViewController.didTapView))
self.view.addGestureRecognizer(tapRecognizer)
}

func didTapView(){
  self.view.endEditing(true)
}

Sunday 18 June 2017

How to center align the cells of a UICollectionView?

https://stackoverflow.com/questions/13588283/how-to-center-align-the-cells-of-a-uicollectionview

Wednesday 14 June 2017

Hiding the tabbar and removing the space

https://stackoverflow.com/questions/37040313/hiding-the-tabbar-and-removing-the-space/37108450

Wednesday 7 June 2017

Swift iOS TableView cell items disappears when scrolling

https://stackoverflow.com/questions/44341768/when-calling-prepareforreuse-in-customtableviewcell-swift-uitableviewcells-be

Execute action when back bar button of UINavigationController is pressed

https://stackoverflow.com/questions/27713747/execute-action-when-back-bar-button-of-uinavigationcontroller-is-pressed


self.navigationItem.hidesBackButton = true
        let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Quoterequestsend.back(sender:)))

        self.navigationItem.leftBarButtonItem = newBackButton




  func back(sender: UIBarButtonItem) {
        
        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        
        let vc = storyBoard.instantiateViewController(withIdentifier: "viewcontroller") as! viewcontroller
        
        self.navigationController?.pushViewController(vc, animated: true)
    }

Monday 5 June 2017

Scrollview and keyboard Swift



@IBOutlet weak var scrollView: UIScrollView!

viewdidload()
{
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)

        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
}

 func keyboardWillShow(notification:NSNotification){
        //give room at the bottom of the scroll view, so it doesn't cover up anything the user needs to tap
        var userInfo = notification.userInfo!
        var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        keyboardFrame = self.view.convert(keyboardFrame, from: nil)
        
        var contentInset:UIEdgeInsets = self.scrollView.contentInset
        contentInset.bottom = keyboardFrame.size.height
        self.scrollView.contentInset = contentInset
    }
    
    func keyboardWillHide(notification:NSNotification){
        let contentInset:UIEdgeInsets = UIEdgeInsets.zero
        self.scrollView.contentInset = contentInset
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false

    }


textfield should be delegate if we want to work with return key function 

Remove bottom line in navigation bar

navigationController ?. navigationBar . setBackgroundImage ( UIImage (), for: . any , barMetrics: . default )          navigat...