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
1dcca06b
Commit
1dcca06b
authored
Oct 30, 2018
by
Orkhan Alikhanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for setting multiplier and priority
parent
a9b84b7a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
5 deletions
+88
-5
Sources/iOS/Layout.swift
+88
-5
No files found.
Sources/iOS/Layout.swift
View file @
1dcca06b
...
...
@@ -83,6 +83,16 @@ public struct Layout {
return
(
constraintable
as?
UIView
)?
.
superview
}
/// Returns the view that is being laied out.
private
var
view
:
UIView
?
{
var
v
=
constraintable
as?
UIView
if
#available(iOS 9.0, *)
,
v
==
nil
{
v
=
(
constraintable
as?
UILayoutGuide
)?
.
owningView
}
return
v
}
/**
An initializer taking Constraintable.
- Parameter view: A Constraintable.
...
...
@@ -94,6 +104,62 @@ public struct Layout {
public
extension
Layout
{
/**
Sets multiplier of the last created constraint.
Not meant for updating the multiplier as it will re-create the constraint.
- Parameter _ multiplier: A CGFloat multiplier.
- Returns: A Layout instance to allow chaining.
*/
func
multiply
(
_
multiplier
:
CGFloat
)
->
Layout
{
return
resetLastConstraint
(
multiplier
:
multiplier
)
}
/**
Sets priority of the last created constraint.
Not meant for updating the multiplier as it will re-create the constraint.
- Parameter _ value: A Float priority.
- Returns: A Layout instance to allow chaining.
*/
func
priority
(
_
value
:
Float
)
->
Layout
{
return
priority
(
.
init
(
rawValue
:
value
))
}
/**
Sets priority of the last created constraint.
Not meant for updating the priority as it will re-create the constraint.
- Parameter _ priority: A UILayoutPriority.
- Returns: A Layout instance to allow chaining.
*/
func
priority
(
_
priority
:
UILayoutPriority
)
->
Layout
{
return
resetLastConstraint
(
priority
:
priority
)
}
/**
Removes the last created constraint and creates new one with the new multiplier and/or priority (if provided).
- Parameter multiplier: An optional CGFloat.
- Parameter priority: An optional UILayoutPriority.
- Returns: A Layout instance to allow chaining.
*/
private
func
resetLastConstraint
(
multiplier
:
CGFloat
?
=
nil
,
priority
:
UILayoutPriority
?
=
nil
)
->
Layout
{
guard
let
v
=
view
?
.
lastConstraint
,
v
.
isActive
else
{
return
self
}
v
.
isActive
=
false
let
newV
=
NSLayoutConstraint
(
item
:
v
.
firstItem
as
Any
,
attribute
:
v
.
firstAttribute
,
relatedBy
:
v
.
relation
,
toItem
:
v
.
secondItem
,
attribute
:
v
.
secondAttribute
,
multiplier
:
multiplier
??
v
.
multiplier
,
constant
:
v
.
constant
)
newV
.
priority
=
priority
??
v
.
priority
newV
.
isActive
=
true
view
?
.
lastConstraint
=
newV
return
self
}
}
public
extension
Layout
{
/**
Constraints top of the view to its parent's.
- Parameter _ offset: A CGFloat offset.
- Returns: A Layout instance to allow chaining.
...
...
@@ -709,16 +775,13 @@ private extension Layout {
}
let
constraint
=
LayoutConstraint
(
fromAnchor
:
from
,
toAnchor
:
to
!
,
constants
:
constants
)
var
v
=
constraintable
as?
UIView
if
#available(iOS 9.0, *)
,
v
==
nil
{
v
=
(
constraintable
as?
UILayoutGuide
)?
.
owningView
}
let
constraints
=
(
v
?
.
constraints
??
[])
+
(
v
?
.
superview
?
.
constraints
??
[])
let
constraints
=
(
v
iew
?
.
constraints
??
[])
+
(
view
?
.
superview
?
.
constraints
??
[])
let
newConstraints
=
constraint
.
constraints
for
newConstraint
in
newConstraints
{
guard
let
activeConstraint
=
constraints
.
first
(
where
:
{
$0
.
equalTo
(
newConstraint
)
})
else
{
newConstraint
.
isActive
=
true
view
?
.
lastConstraint
=
newConstraint
continue
}
...
...
@@ -742,3 +805,23 @@ private extension NSLayoutConstraint {
&&
secondAttribute
==
other
.
secondAttribute
}
}
/// A memory reference to the lastConstraint of UIView.
private
var
LastConstraintKey
:
UInt8
=
0
private
extension
UIView
{
/**
The last consntraint that's created by Layout system.
Used to set multiplier/priority on the last constraint.
*/
var
lastConstraint
:
NSLayoutConstraint
?
{
get
{
return
AssociatedObject
.
get
(
base
:
self
,
key
:
&
LastConstraintKey
)
{
nil
}
}
set
(
value
)
{
AssociatedObject
.
set
(
base
:
self
,
key
:
&
LastConstraintKey
,
value
:
value
)
}
}
}
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