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
217
votes
4 answers

Autoresizing masks programmatically vs Interface Builder / xib / nib

I was in an (probably false) assumption that enabling the right margin indicator in xib is equivalent to using UIViewAutoresizingFlexibleLeftMargin inside code and so on. So, I used to think according to this snapshot: Later today I had to cross…
Raj Pawan Gumdal
  • 7,390
  • 10
  • 60
  • 92
213
votes
6 answers

How to use UIVisualEffectView to Blur Image?

Could someone give a small example of applying the blur to an image? I've been trying to figure out the code for a while now :( still new at obj c! The UIVisualEffectView provides a simple abstraction over complex visual effects. Depending on…
cNoob
  • 2,133
  • 3
  • 13
  • 4
201
votes
4 answers

How do I apply a perspective transform to a UIView?

I'm looking to perform a perspective transform on a UIView (such as seen in coverflow) Does anyonew know if this is possible? I've investigated using CALayer and have run through all the pragmatic programmer Core Animation podcasts, but I'm still…
Nick Cartwright
  • 8,334
  • 15
  • 45
  • 56
201
votes
9 answers

Is it possible to set UIView border properties from interface builder?

Is it possible to control UIView border properties (color, thickness, etc...) directly from interface builder or I can only do it programmatically?
matteo.cajani
  • 2,495
  • 4
  • 21
  • 19
200
votes
29 answers

Get to UIViewController from UIView?

Is there a built-in way to get from a UIView to its UIViewController? I know you can get from UIViewController to its UIView via [self view] but I was wondering if there is a reverse reference?
bertrandom
  • 2,131
  • 2
  • 13
  • 8
198
votes
8 answers

UIlabel layer.cornerRadius not working in iOS 7.1

I'm currently looking at a UILabel with the property addMessageLabel.layer.cornerRadius = 5.0f; On a device with iOS 7.0 installed, it has rounded corners. On a device with iOS 7.1 installed, it does not have rounded corners. Is this just a bug with…
mverderese
  • 5,314
  • 6
  • 27
  • 36
196
votes
3 answers

UIView's frame, bounds, center, origin, when to use what?

UIView has the properties frame, bounds, center, and origin, and they all seem to be interrelated. Most of the time, I deal with frame when setting the position and size of a UIView. I understand that frame is using global coordinate system and…
Boon
  • 40,656
  • 60
  • 209
  • 315
185
votes
26 answers

Dashed line border around UIView

How do I add dashed line border around UIView. Something Like this
Sohaib
  • 10,941
  • 9
  • 32
  • 34
184
votes
30 answers

Load a UIView from nib in Swift

Here is my Objective-C code which I'm using to load a nib for my customised UIView: -(id)init{ NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"myXib" owner:self options:nil]; return [subviewArray objectAtIndex:0]; } What is…
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
171
votes
13 answers

iOS - forward all touches through a view

I have a view overlayed on top of many other views. I am only using the overaly to detect some number of touches on the screen, but other than that I don't want the view to stop the behavior of other views underneath, which are scrollviews, etc. How…
sol
  • 6,402
  • 13
  • 47
  • 57
168
votes
4 answers

Proper practice for subclassing UIView?

I'm working on some custom UIView-based input controls, and I'm trying to ascertain proper practice for setting up the view. When working with a UIViewController, it's fairly simple to use the loadView and related viewWill, viewDid methods, but when…
Moshe
  • 57,511
  • 78
  • 272
  • 425
167
votes
35 answers

How to add a border just on the top side of a UIView

My question is on the title. I don't know how to add a border in a specific side, top or bottom, any side... layer.border draws the border for the whole view...
manonthemoon
  • 2,611
  • 8
  • 26
  • 40
167
votes
12 answers

Given a view, how do I get its viewController?

I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the UIViewController, right?
mahboudz
  • 39,196
  • 16
  • 97
  • 124
161
votes
5 answers

How do I write a custom init for a UIView subclass in Swift?

Say I want to init a UIView subclass with a String and an Int. How would I do this in Swift if I'm just subclassing UIView? If I just make a custom init() function but the parameters are a String and an Int, it tells me that "super.init() isn't…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
160
votes
8 answers

iOS: verify if a point is inside a rect

Is there a way to verify if a CGPoint is inside a specific CGRect? An example would be: I'm dragging a UIImageView and I want to verify if its central point CGPoint is inside another UIImageView.
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241