Questions tagged [uiview]

UIView is a class in the UIKit framework of iOS defines a rectangular area on the screen and the interfaces for managing the content in that area. All UI elements are either subclasses of UIView or are contained within a UIView.

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabel object draws a text string and a UIImageView object draws an image.

Because view objects are the main way your application interacts with the user, they have a number of responsibilities. Here are just a few:

Drawing and animation

  • Views draw content in their rectangular area using technologies such as UIKit, Core Graphics, and OpenGL ES.

  • Some view properties can be animated to new values.

Layout and subview management

  • A view may contain zero or more subviews.

  • Each view defines its own default resizing behavior in relation to its parent view.

  • A view can define the size and position of its subviews as needed.

Event handling

  • A view is a responder and can handle touch events and other events defined by the UIResponder class.
  • Views can use the addGestureRecognizer: method to install gesture recognizers to handle common gestures.

Views can embed other views and create sophisticated visual hierarchies. This creates a parent-child relationship between the view being embedded (known as the subview) and the parent view doing the embedding (known as the superview). Normally, a subview’s visible area is not clipped to the bounds of its superview, but in iOS you can use the clipsToBounds property to alter that behavior. A parent view may contain any number of subviews but each subview has only one superview, which is responsible for positioning its subviews appropriately.

The geometry of a view is defined by its frame, bounds, and center properties. The frame defines the origin and dimensions of the view in the coordinate system of its superview and is commonly used during layout to adjust the size or position of the view. The center property can be used to adjust the position of the view without changing its size. The bounds defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing code. The size portion of the frame and bounds rectangles are coupled together so that changing the size of either rectangle updates the size of both.

For detailed information about how to use the UIView class, see View Programming Guide for iOS.

Creating a View

To create a view programmatically, you can use code like the following:

CGRect  viewRect = CGRectMake(10, 10, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];

Equivalent in Swift is:

let viewRect = CGRectMake(10, 10, 100, 100)
let myView = UIView(frame: viewRect)
18705 questions
90
votes
9 answers

Draw line in UIView

I need to draw a horizontal line in a UIView. What is the easiest way to do it. For example, I want to draw a black horizontal line at y-coord=200. I am NOT using Interface Builder.
John Smith
  • 12,491
  • 18
  • 65
  • 111
89
votes
11 answers

Draw dotted (not dashed!) line, with IBDesignable in 2017

It's easy to draw a dashed line with UIKit. So: CGFloat dashes[] = {4, 2}; [path setLineDash:dashes count:2 phase:0]; [path stroke]; Is there any way way to draw a genuine dotted line?
Fattie
  • 27,874
  • 70
  • 431
  • 719
88
votes
4 answers

Difference between addSubview and insertSubview in UIView class

What is the difference between addSubview and insertSubView methods when a view is added programmatically?
Ashwani K
  • 7,880
  • 19
  • 63
  • 102
87
votes
18 answers

How can I loop through all subviews of a UIView, and their subviews and their subviews

How can I loop through all subviews of a UIView, and their subviews and their subviews?
123hal321
  • 2,080
  • 4
  • 24
  • 25
86
votes
6 answers

Why masksToBounds = YES prevents CALayer shadow?

With the following snippet, I'm adding a drop shadow effect to one my UIView. Which works pretty well. But as soon as I set the view's masksToBounds property to YES. The drop shadow effect isn't rendered any more. self.myView.layer.shadowColor =…
jchatard
  • 1,881
  • 2
  • 20
  • 25
84
votes
10 answers

UIGestureRecognizer blocks subview for handling touch events

I'm trying to figure out how this is done the right way. I've tried to depict the situation: I'm adding a UITableView as a subview of a UIView. The UIView responds to a tap- and pinchGestureRecognizer, but when doing so, the tableview stops…
andershqst
  • 1,454
  • 1
  • 14
  • 24
84
votes
6 answers

Can UIView be copied?

Simply using this way: UIView* view2 = [view1 copy]; // view1 existed This will cause simulator can not launch this app. Try retain, UIView* view2 = [view1 retain]; // view1 existed // modify view2 frame etc Any modifications to view2 will apply…
Forrest
  • 122,703
  • 20
  • 73
  • 107
84
votes
4 answers

resize superview after subviews change dynamically using autolayout

I cant for the love of god the the hang of this resizing superview. I have a UIView *superview with 4 UILabels. 2 function as header for the 2 others. The content in all 4 are dynamic coming from database. SizeToFit vs SizeThatFits:(CGSize) vs…
Pedroinpeace
  • 1,419
  • 1
  • 14
  • 20
84
votes
8 answers

How do you draw a line programmatically from a view controller?

I have a UIViewController. How do I draw a line in one of its programmatically created views?
mkc842
  • 2,361
  • 2
  • 26
  • 38
83
votes
1 answer

UIView - How to get notified when the view is loaded?

Is there anything similar to the viewDidLoad of UIViewController for a UIView??? I need to be notified as soon as a UIView has loaded (Subclass of UIView), and perform some actions.
aryaxt
  • 76,198
  • 92
  • 293
  • 442
83
votes
7 answers

Reuse a uiview xib in storyboard

I typically like to create and design my uiviews in interface builder. Sometimes I need to create a single view in a xib that can be reused in multiple view controllers in a storyboard.
Garfbargle
  • 3,222
  • 2
  • 17
  • 17
82
votes
9 answers

How can I do Key Value Observing and get a KVO callback on a UIView's frame?

I want to watch for changes in a UIView's frame, bounds or center property. How can I use Key-Value Observing to achieve this?
hfossli
  • 22,616
  • 10
  • 116
  • 130
82
votes
6 answers

Creating a reusable UIView with xib (and loading from storyboard)

OK, there are dozens of posts on StackOverflow about this, but none are particularly clear on the solution. I'd like to create a custom UIView with an accompanying xib file. The requirements are: No separate UIViewController – a completely…
Ken Chatfield
  • 3,277
  • 3
  • 22
  • 27
78
votes
8 answers

How do I change UIView Size?

I have trouble with modifying my UIView height at launch. I have to UIView and I want one to be screen size * 70 and the other to fill the gap. here is what I have @IBOutlet weak var questionFrame: UIView! @IBOutlet weak var answerFrame: UIView! …
SKYnine
  • 2,708
  • 7
  • 31
  • 41
77
votes
7 answers

Adding padding to an UIView

I'm looking for a way to add a padding property to an UIView. Ideally, I would like to avoid subclassing and putting it in a category. The usage would be something like: myview.padding = UIEdgeInsetsMake(10, 10, 10, 10); And maybe have a paddingBox…
Meda
  • 2,776
  • 4
  • 20
  • 31