in Swift4, replacement of navigationItem.leftBarButtonItem resizes unwanted - ios

Having a problem with UIBarButtonItem resizing when returning to VC with different values for frame and image.
var selectedR = 1
var leftFrame = CGRect()
var leftImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
if selectedR == 0
{
leftFrame = CGRect(x: 0, y: 0, width: 33, height: 33)
leftImage = UIImage(named: “pic-0”)!
}
else if selectedR == 1
{
leftFrame = CGRect(x: 0, y: 0, width: 79, height: 33)
leftImage = UIImage(named: "pic-1")!
}
else if selectedR == 2
{
leftFrame = CGRect(x: 0, y: 0, width: 34, height: 33)
leftImage = UIImage(named: "pic-2”)!
}
let cButton = UIButton(frame: leftFrame)
cButton.setImage(leftImage, for: UIControlState())
cButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
cButton.contentMode = UIViewContentMode.scaleAspectFit
let leftItem = UIBarButtonItem(customView: cButton)
self.navigationItem.leftBarButtonItem = leftItem
}
First time perfect size for selectedR = 1
Coming back into viewDidLoad with different selectedR value and image blows up, distorts and setting the leftFrame size is not honoured.
This all worked perfect in swift 3, but swift 4 it goes all over the place. Any help or suggestions would be much appreciated.

One major change between iOS 10 and 11 is now bar button items are laid out by the auto layout engine. Typically that should be backward compatible. But if you are having problems with laying out bar button items, you should try setting constraints and see if that solves your problem.
In you case you would say:
var selectedR = 1
var leftFrame = CGRect()
var leftImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
if selectedR == 0
{
leftFrame = CGRect(x: 0, y: 0, width: 33, height: 33)
leftImage = UIImage(named: “pic-0”)!
}
else if selectedR == 1
{
leftFrame = CGRect(x: 0, y: 0, width: 79, height: 33)
leftImage = UIImage(named: "pic-1")!
}
else if selectedR == 2
{
leftFrame = CGRect(x: 0, y: 0, width: 34, height: 33)
leftImage = UIImage(named: "pic-2”)!
}
let cButton = UIButton(frame: leftFrame)
cButton.widthAnchor.constraint(equalToConstant: leftFrame.width).isActive = true
cButton.heightAnchor.constraint(equalToConstant: leftFrame.height).isActive = true
cButton.setImage(leftImage, for: UIControlState())
cButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
cButton.contentMode = UIViewContentMode.scaleAspectFit
let leftItem = UIBarButtonItem(customView: cButton)
self.navigationItem.leftBarButtonItem = leftItem
}
This was covered in Apple's WWDC 2017 session Updating Your App for iOS 11.

Related

How to make "safe area" in UITextField with SecureTextEntry toggle?

I have a problem, I added secureTextEntry toggle in my text field, but text is on toggle button.
extension UITextField {
fileprivate func setPasswordToggleImage(_ button: UIButton) {
if(isSecureTextEntry){
button.setImage(UIImage(named: "eye-active"), for: .normal)
}else{
button.setImage(UIImage(named: "eye-inactive"), for: .normal)
}
}
func enablePasswordToggle(){
let button = UIButton(type: .custom)
setPasswordToggleImage(button)
button.frame = CGRect(x: CGFloat(self.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.addTarget(self, action: #selector(self.togglePasswordView), for: .touchUpInside)
self.rightView = button
self.rightViewMode = .always
}
#IBAction func togglePasswordView(_ sender: Any) {
self.isSecureTextEntry.toggle()
setPasswordToggleImage(sender as! UIButton)
}
}
Also I have a bug that places my icon in far right, instead of the place I meant and I don't know why but it's 2nd icon
You can use this directly to add image and padding to your UITextField. Can be done from code as well as storyboard
// MARK: - UITextField
extension UITextField {
#IBInspectable var leftPadding: CGFloat {
get {
return leftView?.frame.size.width ?? 0.0
}
set {
let frame = CGRect(x: 0, y: 0, width: newValue, height: bounds.size.height)
leftView = UIView(frame: frame)
leftViewMode = .always
}
}
#IBInspectable var leftImage: UIImage? {
get {
let imgView = leftView?.subviews.first(where: {$0 is UIImageView}) as? UIImageView
return imgView?.image
}
set {
let frame = CGRect(x: 0, y: 0, width: leftPadding, height: bounds.size.height)
leftView = UIView(frame: frame)
let imgView = UIImageView(frame: frame.insetBy(dx: 4, dy: 4))
imgView.contentMode = .scaleAspectFit
leftView?.addSubview(imgView)
imgView.image = newValue
leftViewMode = .always
}
}
#IBInspectable var rightPadding: CGFloat {
get {
return rightView?.frame.size.width ?? 0.0
}
set {
let frame = CGRect(x: 0, y: 0, width: newValue, height: bounds.size.height)
rightView = UIView(frame: frame)
rightViewMode = .always
}
}
#IBInspectable var rightImage: UIImage? {
get {
let imgView = rightView?.subviews.first(where: {$0 is UIImageView}) as? UIImageView
return imgView?.image
}
set {
let frame = CGRect(x: 0, y: 0, width: rightPadding, height: bounds.size.height)
rightView = UIView(frame: frame)
let imgView = UIImageView(frame: frame.insetBy(dx: 4, dy: 4))
imgView.contentMode = .scaleAspectFit
rightView?.addSubview(imgView)
imgView.image = newValue
rightViewMode = .always
}
}
}

NavigationItem.titleView is not getting the right size

I'm trying to set a custom view on a large navigationBar, but it insists on staying at 44px.
func setLargeNavBarForSection(_ section: String, subTitle: String, studyLogoName: String) -> Void {
if let navigationBar = self.navigationController?.navigationBar {
navigationBar.barTintColor = .cyan
navigationBar.shadowImage = UIImage()
let customView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBar.frame.width, height: 96.0))
let sectionNameFrame = CGRect(x: 80, y : 23, width : 150, height : 41)
let subTitleFrame = CGRect(x: 80, y: 53, width: 200, height: 14)
let studyImageView = UIImageView(frame: CGRect(x: 20, y: 25, width: 45, height: 45))
studyImageView.image = UIImage(named: studyLogoName)?.imageWithInsets(insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
studyImageView.contentMode = .scaleAspectFit
studyImageView.backgroundColor = .white
studyImageView.layer.cornerRadius = 22
studyImageView.addShadow(radius: 3, opacity: 0.1, x: 1, y: 2)
let myStudiesButton = UIButton(frame: CGRect(x: navigationBar.frame.width-96, y: 36, width: 76, height: 26))
myStudiesButton.setImage(UIImage(named: "switchStudies"), for: .normal)
myStudiesButton.addTarget(self, action: Selector(("switchStudies")), for: .touchUpInside)
myStudiesButton.showsTouchWhenHighlighted = true
let sectionNameLabel = UILabel(frame: sectionNameFrame)
sectionNameLabel.text = section
sectionNameLabel.textAlignment = .left
sectionNameLabel.font = UIFont(name: "Montserrat-Bold", size: 18)
sectionNameLabel.textColor = .black
let subTitleLabel = UILabel(frame: subTitleFrame)
subTitleLabel.text = subTitle
subTitleLabel.textAlignment = .left
subTitleLabel.font = UIFont(name: "Montserrat-Medium", size: 11)
subTitleLabel.textColor = .blueGrey
// customView.addSubview(studyImageView)
// customView.addSubview(myStudiesButton)
// customView.addSubview(sectionNameLabel)
// customView.addSubview(subTitleLabel)
navigationItem.largeTitleDisplayMode = .always
customView.backgroundColor = .red
navigationItem.titleView = customView
}
}
navigationController?.navigationBar.prefersLargeTitles = true get called in the viewDidLoad on the ViewController.

Custom NavigationBar in iOS Swift

I'm creating custom navigationbar in swift for every screen. I'm calling this function at viewWillAppear while first time loading it is coming properly if First Time Loading Screen. I move to another screen it is changing. Next View screen navigation title view is shifted. if I came back to previous screen then also it was shiftedenter image description here. I'm unable to find the bug where code was wrong.left and right navigation items are placing fine . But navigationtitle view only getting shifting.
extension UIViewController {
func reightMenuItems(_ isMenuBtn: Bool, _ navigationItem:UINavigationItem) {
if isMenuBtn {
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "MenuIcon"))
menuImage.contentMode = .scaleAspectFit
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 32)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
followButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)
followButton.addTarget(self, action: #selector(self.btnClickMenu), for: .touchUpInside)
leftView.addSubview(followButton)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}else{
let leftView = UIView()
leftView.backgroundColor = .clear
leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "arrow_white-left"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 12.5, y: 10.5, width: 15, height: 25)
leftView.addSubview(menuImage)
let followButton = UIButton(type: .system)
followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
leftView.addSubview(followButton)
followButton.addTarget(self, action: #selector(self.btnClickBack), for: .touchUpInside)
navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
}
}
fileprivate func leftMenuItems(_ isCart: Bool, _ navigationItem: UINavigationItem) {
//lecftButtons
if isCart {
let reightView = UIView()
reightView.backgroundColor = .clear
reightView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
let menuImage = UIImageView(image:#imageLiteral(resourceName: "Cart"))
menuImage.contentMode = .scaleToFill
menuImage.frame = CGRect(x: 2.5, y: 6, width: 35, height: 34)
reightView.addSubview(menuImage)
let cartBtn = UIButton(type: .system)
cartBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
reightView.addSubview(cartBtn)
cartBtn.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: reightView)]
}else{
let composeButton = BtnFontBold20()
composeButton.setTitle("Done", for: .normal)
composeButton.titleLabel?.textColor = .white
composeButton.frame = CGRect(x: 0, y: 00, width: 34, height: 44)
composeButton.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)
composeButton.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton)]
}
}
func setupNavItems(_ navigationItem: UINavigationItem , day:String = "",address:String = "",type:Int,isMenuBtn:Bool,isCart:Bool,title:String = "") {
//NavigationItem
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setValue(true, forKey: "hidesShadow")
//Shadow
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 1.5, height: 1.5)
self.navigationController?.navigationBar.layer.shadowRadius = 2.0
self.navigationController?.navigationBar.layer.shadowOpacity = 0.5
//reight Menu Items
reightMenuItems(isMenuBtn, navigationItem)
leftMenuItems(isCart, navigationItem)
let height = navigationController?.navigationBar.frame.height ?? 0
let leftButtonWidth: CGFloat = 90 // left padding
let width2 = AppConstants.screenWidth - leftButtonWidth
print(width2)
let Titleview = UIView(frame: CGRect(x: 0, y: 0, width: width2, height: height))
Titleview.backgroundColor = .clear
navigationItem.titleView = Titleview
if type == 1 {
let dayLbl = FontBold18(frame: CGRect(x: 0, y: 0, width: min(width2/2,40), height: height))
dayLbl.text = day
dayLbl.textColor = .white
dayLbl.textAlignment = .left
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
let DirectImag = UIImageView()
DirectImag.contentMode = .scaleAspectFit
DirectImag.image = #imageLiteral(resourceName: "RightDirection")
Titleview.addSubview(DirectImag)
DirectImag.translatesAutoresizingMaskIntoConstraints = false
DirectImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
DirectImag.leadingAnchor.constraint(equalTo: dayLbl.trailingAnchor, constant:5).isActive = true
DirectImag.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
let dayBtn = UIButton(type: .system)
dayBtn.addTarget(self, action: #selector(self.btnClickASAPType), for: .touchUpInside)
Titleview.addSubview(dayBtn)
dayBtn.translatesAutoresizingMaskIntoConstraints = false
dayBtn.centerYAnchor.constraint(equalTo: dayLbl.centerYAnchor).isActive = true
dayBtn.centerXAnchor.constraint(equalTo: dayLbl.centerXAnchor, constant: 0).isActive = true
dayBtn.heightAnchor.constraint(equalTo: dayLbl.heightAnchor).isActive = true
dayBtn.widthAnchor.constraint(equalTo: dayLbl.widthAnchor , constant:25).isActive = true
let addLbl = FontBold18()
addLbl.textColor = .white
addLbl.textAlignment = .left
addLbl.numberOfLines = 1
Titleview.addSubview(addLbl)
addLbl.text = address
addLbl.translatesAutoresizingMaskIntoConstraints = false
addLbl.leadingAnchor.constraint(equalTo: DirectImag.trailingAnchor,constant:5).isActive = true
addLbl.heightAnchor.constraint(equalToConstant: height).isActive = true
addLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
addLbl.widthAnchor.constraint(lessThanOrEqualToConstant: (width2 - dayLbl.frame.width) * 0.9).isActive = true
let downImag = UIImageView()
downImag.contentMode = .scaleAspectFit
downImag.image = #imageLiteral(resourceName: "down-arrow-white")
Titleview.addSubview(downImag)
downImag.translatesAutoresizingMaskIntoConstraints = false
downImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
downImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
downImag.leadingAnchor.constraint(equalTo: addLbl.trailingAnchor, constant:5).isActive = true
downImag.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
let addressBtn = UIButton(type: .system)
addressBtn.backgroundColor = .clear
addressBtn.addTarget(self, action: #selector(self.btnClickAddress), for: .touchUpInside)
Titleview.addSubview(addressBtn)
addressBtn.translatesAutoresizingMaskIntoConstraints = false
addressBtn.centerYAnchor.constraint(equalTo: addLbl.centerYAnchor).isActive = true
addressBtn.leadingAnchor.constraint(equalTo: addLbl.leadingAnchor, constant: 0).isActive = true
addressBtn.heightAnchor.constraint(equalTo: addLbl.heightAnchor).isActive = true
addressBtn.widthAnchor.constraint(equalTo: addLbl.widthAnchor).isActive = true
}
else if type == 2{
let dayLbl = FontBold18()
dayLbl.text = title
dayLbl.textColor = .white
dayLbl.textAlignment = .center
dayLbl.numberOfLines = 1
Titleview.addSubview(dayLbl)
dayLbl.translatesAutoresizingMaskIntoConstraints = false
dayLbl.centerXAnchor.constraint(equalTo: Titleview.centerXAnchor).isActive = true
dayLbl.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
}
}
#objc func btnClickASAPType() {
print("DayExt")
// self.overriderFunc()
}
#objc func btnClickAddress() {
print("AddressExt")
}
#objc func btnClickCart() {
print("CartExt")
}
#objc func btnClickMenu() {
print("MenuExt")
}
#objc func btnClickBack() {
print("backExt")
self.navigationController?.popViewController(animated: true)
}
}
calling setupNavigation Method in viewDidLoad
self.setupNavItems( self.navigationItem, day:"Today", address:"Jayanagar 4th Block", type: 1,isMenuBtn: false,isCart: true)

How to add multiple button in a scrollView programmatically

I am trying to put multiple buttons in a scrollView but it just scrolls background view.
class HomeVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
let scrollView = UIScrollView()
let view = UIView()
scrollView.frame = self.view.bounds
self.view.backgroundColor = .green
scrollView.backgroundColor = .blue
scrollView.addSubview(view)
self.view.addSubview(scrollView)
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3)
view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
view.backgroundColor = .yellow
attractivePlaceButtonSetup()
eatDrinkButtonSetup()
ShoppingButtonSetup()
festivalEventButtonSetup()
hotelGuestHouseButtonSetup()
travellerEssentialButtonSetup()
dealButtonSetup()
seeDoButtonSetup()
}
I wrote this code for button frame
func eatDrinkButtonSetup(){
let button = UIButton()
button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150)
button.setTitle("Eat & Drink", for: .normal)
button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal)
button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0)
button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside)
view.addSubview(button)
}
}
I also try to such way but it just scroll a button.
scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
//Try this...
//Background Scroll Creation
var stickersScrollViewCount = 0
func stickersScrollContents() {
var xCoord: CGFloat = 5
let yCoord: CGFloat = 5
let buttonWidth:CGFloat = 45.0
let buttonHeight: CGFloat = 45.0
let gapBetweenButtons: CGFloat = 5
for i in 0..<stickersImageArray.count{
stickersScrollViewCount = i
// Button properties
let filterButton = UIButton(type: .custom)
filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
filterButton.tag = stickersScrollViewCount
filterButton.backgroundColor = UIColor.clear
filterButton.setTitleColor(UIColor.white, for: .normal)
filterButton.titleLabel?.adjustsFontSizeToFitWidth = true
filterButton.showsTouchWhenHighlighted = true
let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount])
filterButton.setImage(myimage, for: .normal)
filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside)
filterButton.layer.cornerRadius = 5
filterButton.clipsToBounds = true
xCoord += buttonWidth + gapBetweenButtons
bgScrollView.addSubview(filterButton)
}
bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord)
}
//Call the function where ever you want viewDidLoad() or on Button Click!!
//Hope this helps!!!

IOS, Swift: creating a split screen with 2 views, with animation

I'm an absolute IOS newbie studying Swift.
I'm trying to create an app with a calendar UI control on top (already got that working), and a listview on the bottom.
Exactly like the picture on the bottom (and I apologise for the huge pic).
Now, like the calendar app, the screen will only show the calendar control, but if you click a button - the listview will reveal itself on the bottom and the screen will become split.
The only way I can think of doing this is to generate the UITableView in code, assigning the current ViewController as both delegate and datasource, and animating both the views (calendar and listview) to grow/shrink to split the screen.
I don't want to use StackView because that's IOS-9 and up.
Is there a better way to do this?
I suspect you're trying to do something like this. Hope it helps:
Swift 2:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRectMake(0, 0, screenWidth, screenWidth)
view1.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRectMake(0, 0, screenWidth, 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.Center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRectMake(0,0,200,100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", forState: .Normal)
btn.addTarget(self, action: "showView2:", forControlEvents: .TouchUpInside)
view1.addSubview(btn)
view2.frame = CGRectMake(0, screenHeight, screenWidth, screenHeight-screenWidth)
view2.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRectMake(0, (screenHeight-screenWidth)/2-25, screenWidth, 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.Center
view2.addSubview(lbl2)
}
var c = 0
func showView2(sender : UIButton) {
UIView.animateWithDuration(1.0, delay: 0.0, options: .CurveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRectMake(0, self.screenWidth, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", forState: .Normal)
}
else {
self.view2.frame = CGRectMake(0, self.screenHeight, self.screenWidth, self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", forState: .Normal)
}
}, completion: { finished in
})
c++
}
Swift 3:
let view2 = UIView()
var screenWidth = CGFloat()
var screenHeight = CGFloat()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenSize: CGRect = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let view1 = UIView()
view1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenWidth)
view1.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
self.view.addSubview(view1)
let lbl1 = UILabel()
lbl1.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 50)
lbl1.text = "This is view 1"
lbl1.textAlignment = NSTextAlignment.center
lbl1.center = view1.center
view1.addSubview(lbl1)
let btn = UIButton()
btn.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
btn.center = CGPoint(x: view1.center.x, y: view1.center.y+60)
btn.setTitle("Show view 2", for: .normal)
btn.addTarget(self, action: #selector(showView2(_:)), for: .touchUpInside)
view1.addSubview(btn)
view2.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: screenHeight-screenWidth)
view2.backgroundColor = UIColor.red.withAlphaComponent(0.5)
self.view.addSubview(view2)
let lbl2 = UILabel()
lbl2.frame = CGRect(x: 0, y: (screenHeight-screenWidth)/2-25, width: screenWidth, height: 50)
lbl2.text = "This is view 2"
lbl2.textAlignment = NSTextAlignment.center
view2.addSubview(lbl2)
}
var c = 0
func showView2(_ sender : UIButton) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseOut, animations: {
if (self.c%2 == 0) {
self.view2.frame = CGRect(x: 0, y: self.screenWidth, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Hide view 2", for: .normal)
}
else {
self.view2.frame = CGRect(x: 0, y: self.screenHeight, width: self.screenWidth, height: self.screenHeight-self.screenWidth)
sender.setTitle("Show view 2", for: .normal)
}
}, completion: { finished in
})
c += 1
}

Resources