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)
18490 questions
605
votes
11 answers

Cocoa: What's the difference between the frame and the bounds?

UIView and its subclasses all have the properties frame and bounds. What's the difference?
mk12
  • 24,644
  • 29
  • 92
  • 132
592
votes
17 answers

How to tell if UIViewController's view is visible

I have a tab bar application, with many views. Is there a way to know if a particular UIViewController is currently visible from within the UIViewController? (looking for a property)
Rob Bonner
  • 8,650
  • 8
  • 32
  • 52
587
votes
21 answers

Giving UIView rounded corners

My login view has a subview which has a UIActivityView and a UILabel saying "Signing In…". This subview has corners which aren't rounded. How can I make them round? Is there any way to do it inside my xib?
itsaboutcode
  • 21,936
  • 42
  • 103
  • 153
503
votes
19 answers

How to add constraints programmatically using Swift

I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically in Swift to a UIView using this code: var new_view:UIView! = UIView(frame: CGRectMake(0, 0, 100,…
Sara Canducci
  • 5,571
  • 5
  • 17
  • 23
471
votes
27 answers

How to set cornerRadius for only top-left and top-right corner of a UIView?

Is there a way to set cornerRadius for only top-left and top-right corner of a UIView? I tried the following, but it end up not seeing the view anymore. UIView *view = [[UIView alloc] initWithFrame:frame]; CALayer *layer = [CALayer…
tom
  • 13,143
  • 18
  • 63
  • 120
420
votes
33 answers

UIView with rounded corners and drop shadow?

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below. I want a custom UIView... : I just wanted a blank white view with rounded…
Aditya Vaidyam
  • 5,899
  • 3
  • 21
  • 26
321
votes
6 answers

UIView frame, bounds and center

I would like to know how to use these properties in the right manner. As I understand, frame can be used from the container of the view I am creating. It sets the view position relative to the container view. It also sets the size of that view. Also…
Lorenzo B
  • 33,006
  • 23
  • 110
  • 185
315
votes
19 answers

Programmatically create a UIView with color gradient

I'm trying to generate a view with a gradient color background (A solid color to transparent) at runtime. Is there a way of doing that?
Allan Jiang
  • 10,075
  • 22
  • 96
  • 153
288
votes
15 answers

How to add a touch event to a UIView?

How do I add a touch event to a UIView? I try: UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease]; [headerView addTarget:self action:@selector(myEvent:)…
Manni
  • 10,948
  • 15
  • 46
  • 67
271
votes
41 answers

How to find topmost view controller on iOS

I've run into a couple of cases now where it would be convenient to be able to find the "topmost" view controller (the one responsible for the current view), but haven't found a way to do it. Basically the challenge is this: Given that one is…
Hot Licks
  • 44,830
  • 15
  • 88
  • 146
258
votes
27 answers

UIView Infinite 360 degree rotation animation?

I'm trying to rotate a UIImageView 360 degrees, and have looked at several tutorials online. I could get none of them working, without the UIView either stopping, or jumping to a new position. How can I achieve this? The latest thing I've tried…
Derek
  • 9,313
  • 7
  • 27
  • 34
257
votes
9 answers

How to set iPhone UIView z index?

I want to move one view on top of another, how can I know the z index of the view, and how to move on to top?
Tattat
  • 14,290
  • 32
  • 84
  • 135
225
votes
8 answers

How can I mimic the bottom sheet from the Maps app?

Can anyone tell me how I can mimic the bottom sheet in the new Maps app in iOS 10? In Android, you can use a BottomSheet which mimics this behaviour, but I could not find anything like that for iOS. Is that a simple scroll view with a content…
qwertz
  • 5,684
  • 9
  • 35
  • 60
220
votes
6 answers

iPhone - Get Position of UIView within entire UIWindow

The position of a UIView can obviously be determined by view.center or view.frame etc. but this only returns the position of the UIView in relation to it's immediate superview. I need to determine the position of the UIView in the entire 320x480…
adam
  • 21,709
  • 20
  • 83
  • 117
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,132
  • 10
  • 55
  • 85
1
2 3
99 100