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
127
votes
6 answers

iOS: How does one animate to new autolayout constraint (height)

I've never worked with autolayout constraints before. I have a small new app I'm working on and noticed that the NIB's views are defaulting to autolayout. So, I figured I'd take the opportunity to work with it and try to figure out where Apple is…
Meltemi
  • 37,979
  • 50
  • 195
  • 293
125
votes
14 answers

Rounded UIView using CALayers - only some corners - How?

In my application - there are four buttons named as follows: Top - left Bottom - left Top - right Bottom - right Above the buttons there is an image view (or a UIView). Now, suppose a user taps on - top - left button. Above image / view should be…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
122
votes
4 answers

How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?

I am currently making an app that will have multiple timers, which are basically all the same. I want to create a custom class that uses all of the code for the timers as well as the layout/animations, so I can have 5 identical timers that operate…
Michael Campsall
  • 4,325
  • 11
  • 37
  • 52
118
votes
11 answers

UIView touch event in controller

How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?
dhaval shah
  • 4,499
  • 10
  • 29
  • 42
118
votes
8 answers

How do I change the color of the text in a UIPickerView under iOS 7?

I'm aware of the pickerView:viewForRow:forComponent:reusingView method, but when using the view it passes in reusingView: how do I change it to use a different text color? If I use view.backgroundColor = [UIColor whiteColor]; none of the views show…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
118
votes
8 answers

Get position of UIView in respect to its superview's superview

I have a UIView, in which I have arranged UIButtons. I want to find the positions of those UIButtons. I am aware that buttons.frame will give me the positions, but it will give me positions only with respect to its immediate superview. Is there is…
Shailesh
  • 3,072
  • 3
  • 24
  • 33
117
votes
8 answers

When can I activate/deactivate layout constraints?

I've set up multiple sets of constraints in IB, and I'd like to programmatically toggle between them depending on some state. There's a constraintsA outlet collection all of which are marked as installed from IB, and a constraintsB outlet collection…
tybro0103
  • 48,327
  • 33
  • 144
  • 170
113
votes
7 answers

How to load a xib file in a UIView

I have been searching everywhere and nothing so far has worked for me. Basically I want to have a .xib file called rootView.xib and inside it I want to have a UIView (lets call it containerView) that takes up only half of the screen (so there would…
Matt
  • 2,920
  • 6
  • 23
  • 33
112
votes
6 answers

Interface Builder: What are the UIView's Layout iOS 6/7 Deltas for?

I just noticed the iOS 6/7 Delta property found under the UIView's structs layout. What is this for and why is this missing from AutoLayout?
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
111
votes
6 answers

What are the differences between a UIView and a CALayer?

Both have most of the same attributes, both support different kind of animations, both represent different data. What are the differences between a UIView and a CALayer?
Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
110
votes
8 answers

CALayers didn't get resized on its UIView's bounds change. Why?

I have a UIView which has about 8 different CALayer sublayers added to its layer. If I modify the view's bounds (animated), then the view itself shrinks (I checked it with a backgroundColor), but the sublayers' size remains unchanged. How to solve…
Geri Borbás
  • 15,810
  • 18
  • 109
  • 172
109
votes
7 answers

What's the best way to add a drop shadow to my UIView

I am trying to add a drop shadow to views that are layered on top of one another, the views collapse allowing content in other views to be seen, in this vein i want to keep view.clipsToBounds ON so that when the views collapse their content is…
Wez
  • 10,555
  • 5
  • 49
  • 63
108
votes
16 answers

Remove all constraints affecting a UIView

I have a UIView which is placed on the screen via several constraints. Some of the constraints are owned by the superview, others are owned by other ancestors (e.g. perhaps the view property of a UIViewController). I want to remove all of these old…
Senseful
  • 86,719
  • 67
  • 308
  • 465
108
votes
17 answers

"From View Controller" disappears using UIViewControllerContextTransitioning

I got one problem and i have described it below. I am using UIViewControllerContextTransitioning for custom transitions. I have 2 view controllers, first view controller and second view controller. Now I want to add second view controller on first…
NiravPatel
  • 3,260
  • 2
  • 21
  • 31
107
votes
2 answers

What is the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews?

Can anyone give a definitive explanation on the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews methods? And an example implementation where all three would be used. Thanks. What gets me confused is that if I send my…
Tarfa
  • 1,145
  • 2
  • 8
  • 8