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
76
votes
7 answers

Drawing UIBezierPath on code generated UIView

I have a UIView added in code at run time. I want to draw a UIBezierPath in it, but does this means i have to override the drawRect for UIView? Or is there another way of drawing to it on the custom made UIView? Here is the code for generating the…
Itzik984
  • 15,968
  • 28
  • 69
  • 107
76
votes
16 answers

UIView's border color in Interface builder doesn't work?

I am trying to set up a view's layer properties via IB. Everything works except for color of the border (property layer.borderColor): I remember running into this problem a year ago and I ended up doing it programatically. And still, I can do this…
0xSina
  • 20,973
  • 34
  • 136
  • 253
75
votes
4 answers

NSAttributedString background color and rounded corners

I have a question regarding rounded corners and text background color for a custom UIView. Basically, I need to achieve an effect like this (image attached - notice the rounded corners on one side) in a custom UIView: I'm thinking the approach to…
codeBearer
  • 5,114
  • 4
  • 25
  • 29
74
votes
5 answers

Passing through touches to UIViews underneath

I have a UIView with 4 buttons on it and another UIView on top of the buttons view. The top most view contains a UIImageView with a UITapGestureRecognizer on it. The behavoir I am trying to create is that when the user taps the UIImageView it…
Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
74
votes
10 answers

How to fill background image of an UIView

I have an UIView and I set a background image in this way: self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sfond-appz.png"]]; My problem is that back-image is not centered inside the view, but it's replayed some…
Jayyrus
  • 12,961
  • 41
  • 132
  • 214
74
votes
8 answers

How to add an UIViewController's view as subview

Note to googlers, this Q-A is now six years out of date! As Micky below and others mention, this is now done on an everyday basis with Containers in iOS. I have a ViewController which controls many subviews. When I click one of the buttons I…
sperumal
  • 749
  • 1
  • 6
  • 3
74
votes
18 answers

Move UIView up when the keyboard appears in iOS

I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How can I make a UITextField move up when the keyboard is present?. It was working fine. But after…
Anatoliy Gatt
  • 2,501
  • 3
  • 26
  • 42
73
votes
13 answers

Simple way to change the position of UIView?

I change the position of a UIView with following codes without changing size of the view. CGRect f = aView.frame; f.origin.x = 100; // new x f.origin.y = 200; // new y aView.frame = f; Is there more simple way to change only the view position?
Seunghoon
  • 5,632
  • 5
  • 35
  • 41
73
votes
15 answers

How to rotate image in Swift?

I am unable to rotate the image by 90 degrees in swift. I have written below code but there is an error and doesn't compile func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage { //Calculate the size of the rotated…
TechChain
  • 8,404
  • 29
  • 103
  • 228
73
votes
11 answers

How to achieve UIButton / UILabel 'padding' in iPhone app

I've got various views in my iPhone application that require padding e.g a custom UIButton with text aligned left, and a UILabel with a background color. This may be a really stupid question, but how can I apply 'padding' to move the text off the…
user210437
73
votes
7 answers

iOS viewDidLoad for UIView

In a ViewController, there is ViewDidLoad to know when the VC has loaded. For a UIView, what method do i have to use when the view loaded? Would this method be called with any init? edit: No XIB, just programmatically.
manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
72
votes
10 answers

Rotate UIView around its center keeping its size

I'm trying to rotate an UIView a few radians but after applying the transformation it doesn't look to be keeping its size. What's the proper way to achieve this? Here's what I'm doing and what I get (Blue box with the arrow is the View I'm trying to…
Diego Acosta
  • 1,737
  • 3
  • 16
  • 21
72
votes
3 answers

How is the relation between UIView's clipsToBounds and CALayer's masksToBounds?

A UIView has a CALayer. That's pretty sure. But both seem to provide something that means the same thing. If I'd set clipsToBounds=YES, would this also set the layer's masksToBounds=YES? Why different names? Anyone knows?
Thanks
  • 40,109
  • 71
  • 208
  • 322
71
votes
17 answers

UIView backgroundColor disappears when UITableViewCell is selected

I have a simple tableViewCell build in interface builder. It contains a UIView which contains an image. Now, when I select the cell, the default blue selection background is shown, but the backgroundColor of my UIView is gone. My UITableViewCell's…
Tycho Pandelaar
  • 7,367
  • 8
  • 44
  • 70
70
votes
4 answers

UIView hide children views when out of bounds

I have a view, lets say 100x100. And it has set of uiviews as subviews: let say 30x30 If a subview has top-left coordinate: (90,90) - I expect to see only part of this subview. But I still see entire area - even if it is out of parents bounds The…
user349302
  • 3,491
  • 7
  • 27
  • 31