I'm working on an application that uses UIView
s as backgrounds to text. I've put the views behind the text in the storyboard
but for some reason some still seem to be in front of the text. Is there a way to send these view to the back programmatically? Here is a screenshot of what im talking about
Asked
Active
Viewed 1.1k times
9

Evgeny Karkan
- 8,782
- 2
- 32
- 38

Ross Sullivan
- 396
- 1
- 3
- 13
3 Answers
17
You can use on of these two:
- (void)bringSubviewToFront:(UIView *)view;
- (void)sendSubviewToBack:(UIView *)view;
Objective-C Usage
view.sendSubviewToBack(subview)
and view.bringSubviewToFront(subview)
SWIFT 4.2 Usage
view.sendSubview(toBack: subview)
view.bringSubview(toFront: subview)
- bringSubviewToFront: Moves the specified subview so that it appears on top of its siblings.
- sendSubviewToBack: Moves the specified subview so that it appears behind its siblings.
https://developer.apple.com/reference/uikit/uiview?language=objc

Harsh
- 2,852
- 1
- 13
- 27
-
1Thanks a ton! I figured there was method i just didn't know the syntax. – Ross Sullivan Aug 13 '16 at 21:22
1
sendSubviewToBack
API is what you want for this task.
Moves the specified subview so that it appears behind its siblings.
This method moves the specified view to the beginning of the array of views in the subviews property.
UIView class reference
Below is a code snippet in Swift
lang of how to call this API:
Swift
yourSuperview.sendSubviewToBack(yourSubview)

Community
- 1
- 1

Evgeny Karkan
- 8,782
- 2
- 32
- 38
0
Swift 5
yourSuperview.sendSubviewToBack(yourSubview)
or
yourSuperview.bringSubviewToFront(yourSubview)

Alexei Mikhailov
- 81
- 11