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
103
votes
10 answers

Avoid animation of UICollectionView after reloadItemsAtIndexPaths

UICollectionView animate items after reloadItemsAtIndexPaths is called (fade animation). Is there a way to avoid this animation? iOS 6
Marcin
  • 3,694
  • 5
  • 32
  • 52
102
votes
4 answers

How to create a UIView bounce animation?

I have the following CATransition for a UIView called finalScoreView, which makes it enter the screen from the top: CATransition *animation = [CATransition animation]; animation.duration = 0.2; animation.type = kCATransitionPush; animation.subtype =…
WunDaii
  • 2,322
  • 4
  • 18
  • 26
101
votes
10 answers

Interaction beyond bounds of UIView

Is it possible for a UIButton (or any other control for that matter) to receive touch events when the UIButton's frame lies outside of it's parent's frame? Cause when I try this, my UIButton doesn't seem to be able to receive any events. How do I…
ThomasM
  • 2,647
  • 3
  • 25
  • 30
101
votes
6 answers

Correct way to load a Nib for a UIView subclass

I am aware this question has been asked before but the answers are contradicting and I am confused, so please don't flame me. I want to have a reusable UIView subclass throughout my app. I want to describe the interface using a nib file. Now let's…
Adam Waite
  • 19,175
  • 22
  • 126
  • 148
100
votes
7 answers

Check if a subview is in a view

I'm making an app where I add a subview to a view using addSubview: on an IBAction. In the same way, when the button with that IBAction is touched again should call removeFromSuperview on that subview added on that IBAction: PSEUDO…
pmerino
  • 5,900
  • 11
  • 57
  • 76
100
votes
7 answers

Swift UIView background color opacity

I have a UIView with a UILabel in it. I want the UIView to have white background color, but with an opacity of 50%. The problem whith setting view.alpha = 0.5 is that the label will have an opacity of 50% as well, so I figured out that it maybe…
user2099024
  • 1,452
  • 4
  • 16
  • 26
98
votes
5 answers

Swift addsubview and remove it

I want to add sub view and remove with one tap. This is my code: To add subview: var testView: UIView = UIView(frame: CGRectMake(0, 0, 320, 568)) testView.backgroundColor = UIColor.blueColor() testView.alpha = 0.5 testView.tag =…
Dasoga
  • 5,489
  • 4
  • 33
  • 40
97
votes
10 answers

Getting reference to the top-most view/window in iOS application

I'm creating a reusable framework for displaying notifications in an iOS application. I'd like the notification views to be added over the top of everything else in the application, sort of like a UIAlertView. When I init the manager that listens…
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
94
votes
22 answers

How to list out all the subviews in a uiviewcontroller in iOS?

I want to list out all the subviews in a UIViewController. I tried self.view.subviews, but not all of the subviews are listed out, for instance, the subviews in the UITableViewCell are not found. Any idea?
user403015
  • 7,209
  • 19
  • 66
  • 100
94
votes
7 answers

iOS White to Transparent Gradient Layer is Gray

I have a CAGradientLayer inserted to the bottom of this small detail view that pops up at the bottom of the app. As you can see, I've set the colors from white to clear, but there's this strange gray tint that is showing up. Any ideas? // Set up…
Eric Gao
  • 3,502
  • 2
  • 19
  • 29
94
votes
13 answers

Determine if UIView is visible to the user?

is it possible to determine whether my UIView is visible to the user or not? My View is added as subview several times into a Tab Bar Controller. Each instance of this view has a NSTimer that updates the view. However I don't want to update a view…
jantimon
  • 36,840
  • 23
  • 122
  • 185
93
votes
12 answers

Add a border outside of a UIView (instead of inside)

If a add a border of a view using code in a view like self.layer.borderColor = [UIColor yellowColor].CGColor; self.layer.borderWidth = 2.0f; the border is added inside the view like the following: the right view is the original view, as you can…
remykits
  • 1,735
  • 4
  • 18
  • 20
92
votes
10 answers

Setting alpha on UIView sets the alpha on its subviews which should not happen

According to the documentation for UIVIew @property(nonatomic) CGFloat alpha The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. This…
Avba
  • 14,822
  • 20
  • 92
  • 192
91
votes
7 answers

How to Convert UIView to PDF within iOS?

There are a lot of resources about how to display a PDF in an App's UIView. What I am working on now is to create a PDF from UIViews. For example, I have a UIView, with subviews like Textviews, UILabels, UIImages, so how can I convert a big UIView…
Cullen SUN
  • 3,517
  • 3
  • 32
  • 33
91
votes
13 answers

Loading an "overlay" when running long tasks in iOS

What is example for loading overlay in Swift IOS application when do a long tasks. Example for loading data from remote server. I googled but not found any answer. Updated: Thanks for @Sebastian Dressler this is simple way. I updated my code and it…
Sonrobby
  • 3,112
  • 8
  • 30
  • 38