Added Android code
[wl-app.git] / iOS / WolneLektury / Extensions / UIView+Ext.swift
1 //
2 //  UIView+Ext.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 30/05/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import Foundation
10
11 import UIKit
12
13 extension UIView{
14     func addConstraintsFllingContainer(toView: UIView){
15         
16         toView.translatesAutoresizingMaskIntoConstraints = false
17         let viewsDictionary:[String: AnyObject] = ["toView": toView]
18         
19         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[toView]|", options: [], metrics: nil, views: viewsDictionary))
20         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[toView]|", options: [], metrics: nil, views: viewsDictionary))
21     }
22     
23     func centerInSuperview() {
24         self.centerHorizontallyInSuperview()
25         self.centerVerticallyInSuperview()
26     }
27     
28     func addConstraingWidth(width: CGFloat){
29         let c: NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: width)
30         addConstraint(c)
31     }
32     
33     func centerHorizontallyInSuperview(){
34         let c: NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: self.superview, attribute: .centerX, multiplier: 1, constant: 0)
35         self.superview?.addConstraint(c)
36     }
37     
38     func centerVerticallyInSuperview(){
39         let c: NSLayoutConstraint = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: self.superview, attribute: .centerY, multiplier: 1, constant: 0)
40         self.superview?.addConstraint(c)
41     }
42     
43     func addDropShadow(height: CGFloat = 2, radius: CGFloat = 3) {
44         layer.masksToBounds = false
45         layer.shadowColor = UIColor.black.cgColor
46         layer.shadowOffset = CGSize(width: 0, height: height)
47         layer.shadowOpacity = 0.2
48         layer.shadowRadius = radius
49     }
50     
51     func rotate(angle: Double) {
52         let radians = angle / 180.0 * Double.pi
53         let rotation = CGAffineTransform.init(rotationAngle:CGFloat(radians))// CGAffineTransformRotate(self.transform, CGFloat(radians));
54         self.transform = rotation
55     }
56 }