UIView
extension UIView
-
View of the root view controller.
Example
po UIView.rootDeclaration
Swift
@objc static var root: UIView { get } -
Declaration
Swift
@objc static var current: UIView { get } -
Returns the view and all its subviews in sequence.
This method provides an easy way to iterate over a (sub-)tree of the view hierarchy. The views are ordered by nesting level: first the view, then all subviews, then all sub-subviews and so on.
Example
po UIView.current.tree() po UIView.current.tree().filter { $0.isHidden } // only hidden views po Array(UIView.current.tree(depth: 2))[2] // the second subiewDeclaration
Swift
func tree(depth: Int = .max) -> UnfoldSequence<UIView, [(UIView, Int)]>Parameters
depthMaximum level of views to visit. For example, pass
1to only visit the view itself or2for the view and its immediate subviews.Return Value
The view and all its subviews in level order.
-
Returns subviews of the given type.
Example
po UIView.current.all(UILabel.self)Declaration
Swift
func all<T>(_: T.Type) -> [T] where T : UIView -
Returns the first subview of a given type.
Optionally a condition predicate can be passed. If, like in the third example, the predicate parameter is typed, the method parameter may be omitted. However either of them must be defined.
Example
po UIView.current.first(UILabel.self) po UIView.current.first(UILabel.self) { $0.text == "Lorem" } po UIView.current.first { (l: UILabel) in l.text == "Lorem" }Declaration
Swift
func first<T>(_ type: T.Type, where predicate: ((T) -> Bool)? = nil) -> T? where T : UIViewParameters
typeSubview type constraint (optional).
predicateCondition hat must be fulfilled by a subview (optional).
Return Value
Returns the first subview of the given type that matches the predicate.
-
Returns the first subview of the calling type in the given view.
This method is an alternatively flavored
UIView.first(_:where:)to not have to type the.self.Example
po UILabel.first(in: UIView.current)Declaration
Swift
@objc static func first(in root: UIView = UIView.current) -> `Self`?
-
A Boolean indicating whether the view contains a given text.
The following properties will be checked:
UILabel.textUITextField.textand.placeholerUITextView.textUIButton.currentTitle
If any of these attributes contains the passed text, the method will return
true. All other cases will returnfalse.Example
po UILabel.first()?.containsText("name")Declaration
Swift
@objc func containsText(_ pattern: String) -> BoolParameters
patternRegex pattern that must be matched.
-
Returns all views that contain the given text.
Traverses the view hierarchy starting at the receiver and returns all views whose
containsText(_:)check returnstrue.Remark
The labels ofUIButtonswill be excluded.See also
containsText(_:)Example
po UIView.current.grep("o")Declaration
Swift
@objc func grep(_ pattern: String) -> [UIView]Parameters
patternRegex pattern that must be matched.
-
Returns all views that contain the given text.
This method is an alternative to
grep(_:)to allow for a different calling style.See also
grep(_:)Example
po UIView.grep("o") po UIView.grep("o", in: UIView.current)Declaration
Swift
@objc static func grep(_ pattern: String, in root: UIView = UIView.root) -> [UIView] -
Returns the first view with the given accessibility identifier.
Traverses the view hierarchy starting at the reciever and returns the first view whose
accessibilityIdentifierequals or contains the passed in identifier.See also
first(in:)Example
po UIView.current.find(byAccessibilityID: "username_input")Declaration
Swift
@objc func find(byAccessibilityID accessibilityIdentifier: String) -> UIView? -
Returns the first view with the given accessibility identifier.
This method is an alternative to
find(byAccessibilityID:)to allow for a different calling style.See also
find(byAccessibilityID:)Example
po UIView.find(byAccessibilityID: "username_input") po UIView.find(byAccessibilityID: "username_input", in: UIView.current)Declaration
Swift
static func find(byAccessibilityID accessibilityIdentifier: String, in root: UIView = UIView.root) -> UIView?
-
Sets the text of the closest
UITextFieldorUITextView.The
.textproperty of the firstUITextFieldorUITextViewfound in the view hieararchy, starting at the receiver, is set to the passed text.See also
first(in:)Example
po UITextField.first()?.enterText("lldo@lurado.com")Declaration
Swift
@objc func enterText(_ text: String) -
Taps the closest button.
A
.touchUpInsideevent is sent to the firstUIButtonfound in the view hiearchy, starting at the receiver.See also
first(in:)Example
po UIButton.first()?.tap()Declaration
Swift
@objc func tap() -
Slides the closest
UISwitchorUISlider.Looks for the first
UISwitchorUISliderin the view hierarchy, starting at the receiver. If aUISwitchis found, a value of0deactivates the it, every value other than0activates it. Don’t pass any value to toggle the switch.A
UISliderwill be set to the vaue you pass.See also
first(in:)Example
po UISwitch.first()?.slide(1) po UISwitch.first()?.slide()Declaration
Swift
func slide(toValue value: Float? = nil)
-
Colors the areas covered by given insets.
Example
po UIView.current.highlight(true, insets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)) po UIView.current.highlight(false)Declaration
Swift
@objc func highlight(_ highlight: Bool, insets: UIEdgeInsets = .zero, color: UIColor = UIColor.red.withAlphaComponent(0.3), identifier: String = "inset_highlight")Parameters
hightlightPass
trueto add the highlight, passfalseto remove it.insetsThe insets to highlight.
colorThe highlight color.
identifierUnique identifier for the highlight. Used to distinguish between multiple highlights that are active at the same time.
-
Highlights the safe area insets of the receiver.
Basically this is a runtime variant of Interface Builder’s Editor > Canvas > Show Layout Rectangles.
Example
po UIView.current.highlightSafeAreaInsets() po UIView.current.highlightSafeAreaInsets(false) po UIView.current.highlightSafeAreaInsets(depth: 5) po UIView.current.highlightSafeAreaInsets(false, depth: .max)Declaration
Swift
@objc func highlightSafeAreaInsets(_ highlight: Bool = true, color: UIColor = UIColor.blue.withAlphaComponent(0.3), depth: Int = 1)Parameters
hightlightPass
trueto add the highlight, passfalseto remove it.colorThe highlight color.
depthLevel of subviews to process. Pass
1to only highlight the insets on the view itself, pass2to also highlight the insets of its subviews and so on. -
Highlights the layout margins of the receiver.
Basically this is a runtime variant of Interface Builder’s Editor > Canvas > Show Layout Rectangles.
Example
po UIView.current.highlightLayoutMargins() po UIView.current.highlightLayoutMargins(false) po UIView.current.highlightLayoutMargins(depth: 2) po UIView.current.highlightLayoutMargins(false, depth: .max)Declaration
Swift
@objc func highlightLayoutMargins(_ highlight: Bool = true, color: UIColor = UIColor.green.withAlphaComponent(0.3), depth: Int = 1)Parameters
hightlightPass
trueto add the highlight, passfalseto remove it.colorThe highlight color.
depthLevel of subviews to process. Pass
1to only highlight the layout margins on the view itself, pass2to also highlight the layout margins of its subviews and so on.
-
Displays visual view changes that have been made while the program execution was paused.
Normally visual changes to a view from LLDB, like changing the background color, don’t show up until the program execution is continued. Such an update can be forced while the program remains paused by calling this method. Internally this method calls
CATransaction.flush().See also
CATransaction.flush()Example
expr UIView.current.backgroundColor = .orange // still not red expr UIView.current.caflush() // now it's redDeclaration
Swift
@objc func caflush() -
Saves a snapshot of the view as image.
The receiving view is rendered as PNG and saved to disk. If no destination path is specified, the image will be saved in the apps temporary directory. On the simulator the image can then directly be opened. As a convenience the necessary
shell opencommand is printed to the console.Example
po UIView.current.screenshot("/some/absolute/path.png") shell open /some/absolute/path.pngDeclaration
Swift
@objc func screenshot(_ path: String? = nil)Parameters
pathDestination path where to save the view screenshot to.
-
Displays a semi transparent image on top of the receiver to be able to visually compare it to a reference design.
An
UIImageViewdisplaying the image will be created and added as a subview. It will be semi transparent so any differences are easy to spot. The content mode is set to.scaleAspectFitto avoid deformations.Remark
This method unfolds its real utility within theproofimageLLDB command.Example
po UIView.current.overlay(UIImage(named: "reference.png"))Declaration
Swift
@objc func overlay(_ image: UIImage? = nil) -> UIImageView?Parameters
imageImage to overlay. Pass
nilto remove the overlay.Return Value
The created
UIImageView
View on GitHub
Install in Dash
UIView Extension Reference