UIView
extension UIView
-
View of the root view controller.
Example
po UIView.root
Declaration
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 subiew
Declaration
Swift
func tree(depth: Int = .max) -> UnfoldSequence<UIView, [(UIView, Int)]>
Parameters
depth
Maximum level of views to visit. For example, pass
1
to only visit the view itself or2
for 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 : UIView
Parameters
type
Subview type constraint (optional).
predicate
Condition 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.text
UITextField.text
and.placeholer
UITextView.text
UIButton.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) -> Bool
Parameters
pattern
Regex 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 ofUIButtons
will be excluded.See also
containsText(_:)
Example
po UIView.current.grep("o")
Declaration
Swift
@objc func grep(_ pattern: String) -> [UIView]
Parameters
pattern
Regex 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
accessibilityIdentifier
equals 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
UITextField
orUITextView
.The
.text
property of the firstUITextField
orUITextView
found 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
.touchUpInside
event is sent to the firstUIButton
found 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
UISwitch
orUISlider
.Looks for the first
UISwitch
orUISlider
in the view hierarchy, starting at the receiver. If aUISwitch
is found, a value of0
deactivates the it, every value other than0
activates it. Don’t pass any value to toggle the switch.A
UISlider
will 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
hightlight
Pass
true
to add the highlight, passfalse
to remove it.insets
The insets to highlight.
color
The highlight color.
identifier
Unique 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
hightlight
Pass
true
to add the highlight, passfalse
to remove it.color
The highlight color.
depth
Level of subviews to process. Pass
1
to only highlight the insets on the view itself, pass2
to 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
hightlight
Pass
true
to add the highlight, passfalse
to remove it.color
The highlight color.
depth
Level of subviews to process. Pass
1
to only highlight the layout margins on the view itself, pass2
to 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 red
Declaration
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 open
command is printed to the console.Example
po UIView.current.screenshot("/some/absolute/path.png") shell open /some/absolute/path.png
Declaration
Swift
@objc func screenshot(_ path: String? = nil)
Parameters
path
Destination 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
UIImageView
displaying 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.scaleAspectFit
to avoid deformations.Remark
This method unfolds its real utility within theproofimage
LLDB command.Example
po UIView.current.overlay(UIImage(named: "reference.png"))
Declaration
Swift
@objc func overlay(_ image: UIImage? = nil) -> UIImageView?
Parameters
image
Image to overlay. Pass
nil
to remove the overlay.Return Value
The created
UIImageView