Pos
public class Pos
Describes a position which can be an absolute value, a percentage, centered, or
relative to the ending dimension. Integer values are implicitly convertible to
an absolute Pos. These objects are created using the static methods percent
,
anchorEnd
and center
. The Pos
objects can be combined with the addition and
subtraction operators.
Use the Pos
objects on the x
or y
properties of a view to control the position.
These can be used to set the absolute position, when merely assigning an
integer value (via the implicit integer to Pos
conversion), and they can be combined
to produce more useful layouts, like: pos.center - 3
, which would shift the postion
of the view 3 characters to the left after centering for example.
It is possible to reference coordinates of another view by using the methods
left(of: view)
, right(of: view)
, bottom(of: View)
, top(of: View)
.
The x(of: View)
and y(of: View)
are
aliases to left(of: View)
and top(of: View)
respectively.
Examples:
label = Label ("Hello world")
label.x = Pos.at (10) // at column 10
label.y = Pos.percent (50) // At 50%
label.x = Pos.center () // center position
// Center relative to another label
label.x = Pos.center () - Dim.width(anotherLabel)
-
Creates a Pos object the represents a percentage of the container’s bounds
This example creates a
TextView
that is centered horizontally, is 50% of the way down is 30% in height and is 80% the width of theView
it is added tolet textView = TextView() textView.x = Pos.center () textView.y = Pos.percent (50) textView.width = Dim.percent (80) textView.height = Dim.percent (30)
Declaration
Swift
public static func percent(n: Float) throws -> Pos
Parameters
n
A value between 0 and 100 representing the percentage.
-
Creates a Pos object that is anchored to the end (right side or bottom)), useful to flush the layout from the right or bottom.
This example shows how to align a
Button
to the bottom-right of theView
anchorButton.x = pos.anchorEnd () - (pos.right (anchorButton) - pos.left (anchorButton)) anchorButton.y = pos.anchorEnd (1)
Declaration
Swift
public static func anchorEnd(margin: Int = 0) -> Pos
Parameters
margin
An optional margin from the end, defaults to zero.
Return Value
the
Pos
object anchored to the end (the bottom or the right side). -
Creates a
Pos
object that represents the center relative to the parentThis creates a
TextField
that is centered horizontally, is 50% of the way down, is 30% the height, and is 80% the width of theView
it added to.var textView = TextView () textView.x = Pos.center (), textView.y = Pos.percent (50), textView.width = Dim.percent (80), textView.height = Dim.percent (30), };
Declaration
Swift
public static func center() -> Pos
Return Value
the center Position object
-
Creates a new
Pos
object for the absolute position specifiedDeclaration
Swift
public static func at(_ n: Int) -> Pos
Parameters
n
a specific location
-
Produces a dimension that adds the two specified positions together
Declaration
Swift
public static func + (lhs: Pos, rhs: Pos) -> Pos
-
Produces a dimension that adds the two specified positions together
Declaration
Swift
public static func + (lhs: Pos, rhs: Int) -> Pos
-
Produces a dimension that subtracts the second position value from the first
Declaration
Swift
public static func - (lhs: Pos, rhs: Pos) -> Pos
-
Produces a dimension that subtracts the second position value from the first
Declaration
Swift
public static func - (lhs: Pos, rhs: Int) -> Pos
-
Creates a position object that references the left-side of the provided view
Declaration
Swift
public static func left(of view: View) -> Pos
-
Creates a position object that references the colum coordinate of the provided view
Declaration
Swift
public static func x(of view: View) -> Pos
-
Creates a position object that references the top (y or row) coordinate of the provided view
Declaration
Swift
public static func top(of view: View) -> Pos
-
Creates a position object that references the y (row) coordinate of the provided view
Declaration
Swift
public static func y(of view: View) -> Pos
-
Creates a position object that references the right side coordinate of the provided view
Declaration
Swift
public static func right(of view: View) -> Pos
-
Creates a position object that references the bottom side coordinate of the provided view
Declaration
Swift
public static func bottom(of view: View) -> Pos