Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
9c040edc
Unverified
Commit
9c040edc
authored
Jun 05, 2016
by
M. Porooshani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated MaterialLayout with modern syntax
parent
d2d0214f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
64 additions
and
52 deletions
+64
-52
Sources/iOS/MaterialLayout.swift
+64
-52
No files found.
Sources/iOS/MaterialLayout.swift
View file @
9c040edc
...
@@ -32,118 +32,130 @@ import UIKit
...
@@ -32,118 +32,130 @@ import UIKit
public
struct
MaterialLayout
{
public
struct
MaterialLayout
{
/// Width
/// Width
public
static
func
width
(
parent
:
UIView
,
child
:
UIView
,
width
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
width
(
parent
:
UIView
,
child
:
UIView
,
width
:
CGFloat
=
0
)
{
let
metrics
:
Dictionary
<
String
,
AnyObject
>
=
[
"width"
:
width
]
prepareForConstraint
([
child
])
let
views
:
Dictionary
<
String
,
AnyObject
>
=
[
"child"
:
child
]
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Width
,
relatedBy
:
.
Equal
,
toItem
:
nil
,
attribute
:
.
Width
,
multiplier
:
1.0
,
constant
:
width
))
parent
.
addConstraints
(
constraint
(
"H:[child(width)]"
,
options
:
options
,
metrics
:
metrics
,
views
:
views
))
}
}
/// Height
/// Height
public
static
func
height
(
parent
:
UIView
,
child
:
UIView
,
height
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
height
(
parent
:
UIView
,
child
:
UIView
,
height
:
CGFloat
=
0
)
{
let
metrics
:
Dictionary
<
String
,
AnyObject
>
=
[
"height"
:
height
]
prepareForConstraint
([
child
])
let
views
:
Dictionary
<
String
,
AnyObject
>
=
[
"child"
:
child
]
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Height
,
relatedBy
:
.
Equal
,
toItem
:
nil
,
attribute
:
.
Height
,
multiplier
:
1.0
,
constant
:
height
))
parent
.
addConstraints
(
constraint
(
"V:[child(height)]"
,
options
:
options
,
metrics
:
metrics
,
views
:
views
))
}
}
/// Size
/// Size
public
static
func
size
(
parent
:
UIView
,
child
:
UIView
,
width
:
CGFloat
=
0
,
height
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
size
(
parent
:
UIView
,
child
:
UIView
,
width
:
CGFloat
=
0
,
height
:
CGFloat
=
0
)
{
MaterialLayout
.
width
(
parent
,
child
:
child
,
width
:
width
)
MaterialLayout
.
width
(
parent
,
child
:
child
,
width
:
width
)
MaterialLayout
.
height
(
parent
,
child
:
child
,
height
:
height
)
MaterialLayout
.
height
(
parent
,
child
:
child
,
height
:
height
)
}
}
/// AlignToParentHorizontally
/// AlignToParentHorizontally
public
static
func
alignToParentHorizontally
(
parent
:
UIView
,
children
:
Array
<
UIView
>
,
left
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
spacing
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignToParentHorizontally
(
parent
:
UIView
,
children
:
Array
<
UIView
>
,
left
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
spacing
:
CGFloat
=
0
)
{
if
0
<
children
.
count
{
prepareForConstraint
(
children
)
var
format
:
String
=
"H:|-(left)-"
if
0
<
children
.
count
{
var
i
:
Int
=
1
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
0
],
attribute
:
.
Left
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Left
,
multiplier
:
1.0
,
constant
:
left
))
var
views
:
Dictionary
<
String
,
UIView
>
=
Dictionary
<
String
,
UIView
>
()
if
1
<
children
.
count
{
for
v
in
children
{
for
i
in
1
...
(
children
.
count
-
1
)
{
let
k
:
String
=
"view
\(
i
)
"
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
i
],
attribute
:
.
Left
,
relatedBy
:
.
Equal
,
toItem
:
children
[
i
-
1
],
attribute
:
.
Right
,
multiplier
:
1.0
,
constant
:
spacing
))
i
+=
1
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
i
],
attribute
:
.
Width
,
relatedBy
:
.
Equal
,
toItem
:
children
[
0
],
attribute
:
.
Width
,
multiplier
:
1.0
,
constant
:
0.0
))
views
[
k
]
=
v
}
format
+=
i
>
children
.
count
?
"[
\(
k
)
(==view1)]-(right)-|"
:
"[
\(
k
)
(==view1)]-(spacing)-"
}
}
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
children
.
count
-
1
],
attribute
:
.
Right
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Right
,
multiplier
:
1.0
,
constant
:
-
right
))
parent
.
addConstraints
(
constraint
(
format
,
options
:
options
,
metrics
:
[
"left"
:
left
,
"right"
:
right
,
"spacing"
:
spacing
],
views
:
views
))
}
}
}
}
/// AlignToParentVertically
/// AlignToParentVertically
public
static
func
alignToParentVertically
(
parent
:
UIView
,
children
:
Array
<
UIView
>
,
top
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
,
spacing
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignToParentVertically
(
parent
:
UIView
,
children
:
Array
<
UIView
>
,
top
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
,
spacing
:
CGFloat
=
0
)
{
if
0
<
children
.
count
{
prepareForConstraint
(
children
)
var
format
:
String
=
"V:|-(top)-"
if
0
<
children
.
count
{
var
i
:
Int
=
1
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
0
],
attribute
:
.
Top
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Top
,
multiplier
:
1.0
,
constant
:
top
))
var
views
:
Dictionary
<
String
,
UIView
>
=
Dictionary
<
String
,
UIView
>
()
if
1
<
children
.
count
{
for
v
in
children
{
for
i
in
1
...
(
children
.
count
-
1
)
{
let
k
:
String
=
"view
\(
i
)
"
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
i
],
attribute
:
.
Top
,
relatedBy
:
.
Equal
,
toItem
:
children
[
i
-
1
],
attribute
:
.
Bottom
,
multiplier
:
1.0
,
constant
:
spacing
))
i
+=
1
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
i
],
attribute
:
.
Height
,
relatedBy
:
.
Equal
,
toItem
:
children
[
0
],
attribute
:
.
Height
,
multiplier
:
1.0
,
constant
:
0.0
))
views
[
k
]
=
v
}
format
+=
i
>
children
.
count
?
"[
\(
k
)
(==view1)]-(bottom)-|"
:
"[
\(
k
)
(==view1)]-(spacing)-"
}
}
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
children
[
children
.
count
-
1
],
attribute
:
.
Bottom
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Bottom
,
multiplier
:
1.0
,
constant
:
-
bottom
))
parent
.
addConstraints
(
constraint
(
format
,
options
:
options
,
metrics
:
[
"top"
:
top
,
"bottom"
:
bottom
,
"spacing"
:
spacing
],
views
:
views
))
}
}
}
}
/// AlignToParentHorizontally
/// AlignToParentHorizontally
public
static
func
alignToParentHorizontally
(
parent
:
UIView
,
child
:
UIView
,
left
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignToParentHorizontally
(
parent
:
UIView
,
child
:
UIView
,
left
:
CGFloat
=
0
,
right
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"H:|-(left)-[child]-(right)-|"
,
options
:
options
,
metrics
:
[
"left"
:
left
,
"right"
:
right
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Left
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Left
,
multiplier
:
1.0
,
constant
:
left
))
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Right
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Right
,
multiplier
:
1.0
,
constant
:
-
right
))
}
}
/// AlignToParentVertically
/// AlignToParentVertically
public
static
func
alignToParentVertically
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignToParentVertically
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"V:|-(top)-[child]-(bottom)-|"
,
options
:
options
,
metrics
:
[
"bottom"
:
bottom
,
"top"
:
top
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Top
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Top
,
multiplier
:
1.0
,
constant
:
top
))
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Bottom
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Bottom
,
multiplier
:
1.0
,
constant
:
-
bottom
))
}
}
/// AlignToParent
/// AlignToParent
public
static
func
alignToParent
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
left
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
alignToParent
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
left
:
CGFloat
=
0
,
bottom
:
CGFloat
=
0
,
right
:
CGFloat
=
0
)
{
alignToParentHorizontally
(
parent
,
child
:
child
,
left
:
left
,
right
:
right
)
alignToParentHorizontally
(
parent
,
child
:
child
,
left
:
left
,
right
:
right
)
alignToParentVertically
(
parent
,
child
:
child
,
top
:
top
,
bottom
:
bottom
)
alignToParentVertically
(
parent
,
child
:
child
,
top
:
top
,
bottom
:
bottom
)
}
}
/// AlignFromTopLeft
/// AlignFromTopLeft
public
static
func
alignFromTopLeft
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
left
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
alignFromTopLeft
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
left
:
CGFloat
=
0
)
{
alignFromTop
(
parent
,
child
:
child
,
top
:
top
)
alignFromTop
(
parent
,
child
:
child
,
top
:
top
)
alignFromLeft
(
parent
,
child
:
child
,
left
:
left
)
alignFromLeft
(
parent
,
child
:
child
,
left
:
left
)
}
}
/// AlignFromTopRight
/// AlignFromTopRight
public
static
func
alignFromTopRight
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
alignFromTopRight
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
right
:
CGFloat
=
0
)
{
alignFromTop
(
parent
,
child
:
child
,
top
:
top
)
alignFromTop
(
parent
,
child
:
child
,
top
:
top
)
alignFromRight
(
parent
,
child
:
child
,
right
:
right
)
alignFromRight
(
parent
,
child
:
child
,
right
:
right
)
}
}
/// AlignFromBottomLeft
/// AlignFromBottomLeft
public
static
func
alignFromBottomLeft
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
,
left
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
alignFromBottomLeft
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
,
left
:
CGFloat
=
0
)
{
alignFromBottom
(
parent
,
child
:
child
,
bottom
:
bottom
)
alignFromBottom
(
parent
,
child
:
child
,
bottom
:
bottom
)
alignFromLeft
(
parent
,
child
:
child
,
left
:
left
)
alignFromLeft
(
parent
,
child
:
child
,
left
:
left
)
}
}
/// AlignFromBottomRight
/// AlignFromBottomRight
public
static
func
alignFromBottomRight
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
,
right
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[]
)
{
public
static
func
alignFromBottomRight
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
,
right
:
CGFloat
=
0
)
{
alignFromBottom
(
parent
,
child
:
child
,
bottom
:
bottom
)
alignFromBottom
(
parent
,
child
:
child
,
bottom
:
bottom
)
alignFromRight
(
parent
,
child
:
child
,
right
:
right
)
alignFromRight
(
parent
,
child
:
child
,
right
:
right
)
}
}
/// AlignFromTop
/// AlignFromTop
public
static
func
alignFromTop
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignFromTop
(
parent
:
UIView
,
child
:
UIView
,
top
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"V:|-(top)-[child]"
,
options
:
options
,
metrics
:
[
"top"
:
top
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Top
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Top
,
multiplier
:
1.0
,
constant
:
top
))
}
}
/// AlignFromLeft
/// AlignFromLeft
public
static
func
alignFromLeft
(
parent
:
UIView
,
child
:
UIView
,
left
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignFromLeft
(
parent
:
UIView
,
child
:
UIView
,
left
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"H:|-(left)-[child]"
,
options
:
options
,
metrics
:
[
"left"
:
left
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Left
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Left
,
multiplier
:
1.0
,
constant
:
left
))
}
}
/// AlignFromBottom
/// AlignFromBottom
public
static
func
alignFromBottom
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignFromBottom
(
parent
:
UIView
,
child
:
UIView
,
bottom
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"V:[child]-(bottom)-|"
,
options
:
options
,
metrics
:
[
"bottom"
:
bottom
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Bottom
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Bottom
,
multiplier
:
1.0
,
constant
:
-
bottom
))
}
}
/// AlignFromRight
/// AlignFromRight
public
static
func
alignFromRight
(
parent
:
UIView
,
child
:
UIView
,
right
:
CGFloat
=
0
,
options
:
NSLayoutFormatOptions
=
[])
{
public
static
func
alignFromRight
(
parent
:
UIView
,
child
:
UIView
,
right
:
CGFloat
=
0
)
{
parent
.
addConstraints
(
constraint
(
"H:[child]-(right)-|"
,
options
:
options
,
metrics
:
[
"right"
:
right
],
views
:
[
"child"
:
child
]))
prepareForConstraint
([
child
])
parent
.
addConstraint
(
NSLayoutConstraint
(
item
:
child
,
attribute
:
.
Right
,
relatedBy
:
.
Equal
,
toItem
:
parent
,
attribute
:
.
Right
,
multiplier
:
1.0
,
constant
:
-
right
))
}
}
/// Constraint
/// prepareForConstraint
private
static
func
prepareForConstraint
(
views
:
[
UIView
])
{
for
v
in
views
{
v
.
translatesAutoresizingMaskIntoConstraints
=
false
}
}
/// Constraint
public
static
func
constraint
(
format
:
String
,
options
:
NSLayoutFormatOptions
,
metrics
:
Dictionary
<
String
,
AnyObject
>
?,
views
:
Dictionary
<
String
,
AnyObject
>
)
->
Array
<
NSLayoutConstraint
>
{
public
static
func
constraint
(
format
:
String
,
options
:
NSLayoutFormatOptions
,
metrics
:
Dictionary
<
String
,
AnyObject
>
?,
views
:
Dictionary
<
String
,
AnyObject
>
)
->
Array
<
NSLayoutConstraint
>
{
for
(
_
,
a
)
in
views
{
for
(
_
,
a
)
in
views
{
if
let
v
:
UIView
=
a
as?
UIView
{
if
let
v
:
UIView
=
a
as?
UIView
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment