UIView
public extension UIView
Auto Layout and general utility methods for UIView.
-
Remove all subviews of the calling
UIView.Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
func removeSubviews() -
Center the calling
UIViewon the X-axis, with optional margins to the right and left.Copyright
©2016 Lionheart Software LLCDate
March 9, 2016Declaration
Swift
@available(iOS 9, *) func centerOnXAxis(width: CGFloat? = nil) -
Center the calling
UIViewon the Y-axis, with optional margins to the top and bottom.Copyright
©2016 Lionheart Software LLCDate
March 9, 2016Declaration
Swift
@available(iOS 9, *) func centerOnYAxis(height: CGFloat? = nil) -
Set the height of this view to a specified value using Auto Layout.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func setHeight(_ height: CGFloat)Parameters
heightA
Floatspecifying the view’s height. -
Set the width of this view to a specified value using Auto Layout.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func setWidth(_ width: CGFloat)Parameters
widthA
Floatspecifying the view’s width. -
Set the size of the view using Auto Layout.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9.0, *) func setContentSize(_ size: CGSize)Parameters
sizeA
CGSizespecifying the view’s size. -
Undocumented
Declaration
Swift
func fillHorizontalMarginsOfSuperview(margin: CGFloat = 0) -
Pin the view’s horizontal edges to the left and right of its superview.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func fillWidthOfSuperview() -
Pin the view’s vertical edges to the top and bottom of its superview.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func fillHeightOfSuperview() -
Pin the view’s horizontal edges to the left and right of its superview with a margin.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func fillWidthOfSuperview(margin: CGFloat)Parameters
marginA
CGFloatrepresenting the horizontal margin. -
Pin the view’s vertical edges to the top and bottom of its superview with a margin.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func fillHeightOfSuperview(margin: CGFloat)Parameters
marginA
CGFloatrepresenting the vertical margin. -
Pin the view’s edges to the top, bottom, left, and right of its superview with a margin.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@available(iOS 9, *) func fillSuperview(_ axis: NSLayoutConstraint.Axis? = nil, margin: CGFloat = 0)Parameters
axisOptional. Specify only when you want to pin the view to a particular axis.
marginA
CGFloatrepresenting the vertical margin. -
Apply the specified visual format constraints to the current view. The “view” placeholder is used in the provided VFL string. Usage can best be illustrated through examples:
Pin a
UIViewto the left and right of its superview with a 20px margin:let view = UIView() view.addVisualFormatConstraints("|-20-[view]-20-|")Set a view to be at least 36px high, and at least 16px from the top of its superview.
view.addVisualFormatConstraints("V:|-(>=16)-[view(>36)]")Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@discardableResult func addVisualFormatConstraints(_ format: String, metrics: LayoutDictionary? = nil) -> [NSLayoutConstraint]Parameters
formatThe format specification for the constraints. For more information, see Visual Format Language in Auto Layout Guide.
-
Apply the VFL format constraints to the specified views, with the provided metrics.
Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
@discardableResult class func addVisualFormatConstraints(_ format: String, metrics: LayoutDictionary? = nil, views: LayoutDictionary) -> [NSLayoutConstraint]Parameters
formatThe format specification for the constraints. For more information, see Visual Format Language in Auto Layout Guide.
metricsA dictionary of constants that appear in the visual format string. The dictionary’s keys must be the string values used in the visual format string. Their values must be NSNumber objects.
viewsA dictionary of views that appear in the visual format string. The keys must be the string values used in the visual format string, and the values must be the view objects.
-
Return the given distance from a view to a specified
CGPoint.Copyright
©2016 Lionheart Software LLCDate
February 17, 2016Declaration
Swift
func distance(toPoint point: CGPoint) -> FloatParameters
pointThe
CGPointto measure the distance to.Return Value
A
Floatrepresenting the distance to the specifiedCGPoint. -
Returns a 1x1
CGRectin the center of the callingUIView.Copyright
©2016 Lionheart Software LLCDate
March 9, 2016Declaration
Swift
var centerRect: CGRect { get }Return Value
A 1x1
CGRect. -
Return an
Arrayof recursive subviews matching the provided test. This method is incredibly helpful in returning all descendants of a given view of a certain type. For instance:let view = UIView() let subview = UILabel() let label1 = UILabel() let label2 = UILabel() view.addSubview(subview) view.addSubview(label1) subview.addSubview(label2) let labels: [UILabel] = view.descendantViewsOfType()labelswill contain bothlabel1andlabel2.Copyright
©2016 Lionheart Software LLCDate
March 9, 2016Declaration
Swift
func descendantViewsOfType<T>(passingTest test: (T) -> Bool = { _ in true }) -> [T]Return Value
An
ArrayofUIViews of typeT. -
This method simply returns the last object in
descendantViewsOfType(passingTest:).Copyright
©2016 Lionheart Software LLCDate
March 9, 2016Declaration
Swift
func lastDescendantViewOfType<T>(passingTest test: (T) -> Bool = { i in true }) -> T?Return Value
A view of type
T.
View on GitHub
UIView Extension Reference