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
158
votes
11 answers

Load view from an external xib file in storyboard

I want to use a view throughout multiple viewcontrollers in a storyboard. Thus, I thought about designing the view in an external xib so changes are reflected in every viewcontroller. But how can one load a view from a external xib in a storyboard…
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
155
votes
7 answers

Event handling for iOS - how hitTest:withEvent: and pointInside:withEvent: are related?

While most apple documents are very well written, I think 'Event Handling Guide for iOS' is an exception. It's hard for me to clearly understand what's been described there. The document says, In hit-testing, a window calls hitTest:withEvent: on…
realstuff02
  • 1,645
  • 4
  • 12
  • 6
153
votes
22 answers

UIView bottom border?

To a UIScrollView *toScrollView (which is the width of the screen), I want to add a gray bottom border (exactly like that of the to-field of the compose view of the iPhone's native Messages app). To achieve this, I followed Cocoa Touch: How To…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
147
votes
24 answers

Handling an empty UITableView. Print a friendly message

I have a UITableView that in some cases it is legal to be empty. So instead of showing the background image of the app, I would prefer to print a friendly message in the screen, such as: This list is now empty What is the simplest way to do it?
cateof
  • 6,608
  • 25
  • 79
  • 153
142
votes
8 answers

iPhone UIView Animation Best Practice

What is considered best practice for animating view transitions on the iPhone? For example, the ViewTransitions sample project from apple uses code like: CATransition *applicationLoadViewIn = [CATransition animation]; [applicationLoadViewIn…
Keith Fitzgerald
  • 5,651
  • 14
  • 43
  • 58
139
votes
16 answers

How Do I Take a Screen Shot of a UIView?

I am wondering how my iPhone app can take a screen shot of a specific UIView as a UIImage. I tried this code but all I get is a blank image. UIGraphicsBeginImageContext(CGSizeMake(320,480)); CGContextRef context =…
cduck
  • 2,691
  • 6
  • 29
  • 35
138
votes
9 answers

How to draw a custom UIView that is just a circle - iPhone app

How would I go about drawing a custom UIView that is literally just a ball (a 2D circle)? Would I just override the drawRect method? And can someone show me the code for drawing a blue circle? Also, would it be okay to change the frame of that view…
StanLe
  • 5,037
  • 9
  • 38
  • 41
138
votes
8 answers

How to control shadow spread and blur?

I have designed UI elements in sketch, and one of them has a shadow with blur 1 and spread 0. I looked at the doc for the views layer property and layer doesn't have anything named spread or blur, or anything equivalent (the only control was merely…
Quantaliinuxite
  • 3,133
  • 4
  • 18
  • 32
137
votes
8 answers

iPhone SDK: what is the difference between loadView and viewDidLoad?

When working with views and view controllers in an iPhone app, can anyone explain the difference between loadView and viewDidLoad? My personal context, is that I build all my views from code, I do not and will not use Interface Builder, should…
ryan.scott
  • 2,215
  • 2
  • 18
  • 16
137
votes
21 answers

Completion block for popViewController

When dismissing a modal view controller using dismissViewController, there is the option to provide a completion block. Is there a similar equivalent for popViewController? The completion argument is quite handy. For instance, I can use it to hold…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
135
votes
12 answers

Changing my CALayer's anchorPoint moves the view

I want to alter the anchorPoint, but keep the view in the same place. I've tried NSLog-ing self.layer.position and self.center and they both stay the same regardless of changes to the anchorPoint. Yet my view moves! Any tips on how to do…
Kenny Winker
  • 11,919
  • 7
  • 56
  • 78
134
votes
24 answers

How to convert a UIView to an image

I want to convert a UIView to an image and save it in my app. Can someone please tell me how to take screenshot of a view or convert it to an image and what is the best way to save it in an app (Not camera roll)? Here is the code for the view: var…
Sameer Hussain
  • 2,421
  • 8
  • 23
  • 41
132
votes
9 answers

Understand convertRect:toView:, convertRect:FromView:, convertPoint:toView: and convertPoint:fromView: methods

I'm trying to understand the functionalities of these methods. Could you provide me with a simple use case to understand their semantics? From the documentation, for example, convertPoint:fromView: method is described as follows: Converts a point…
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
129
votes
2 answers

How does clipsToBounds work?

I would like to know how to use the UIView property clipsToBounds. The official documentation says the following: clipsToBounds property A Boolean value that determines whether subviews are confined to the bounds of the view. Discussion Setting…
Toshi
  • 6,012
  • 8
  • 35
  • 58
128
votes
6 answers

What is the most robust way to force a UIView to redraw?

I have a UITableView with a list of items. Selecting an item pushes a viewController that then proceeds to do the following. from method viewDidLoad I fire off a URLRequest for data that is required by on of my subviews - a UIView subclass with…
dugla
  • 12,774
  • 26
  • 88
  • 136