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
6
votes
1 answer

drawing with clear color on UIView (cutting a hole) in static method

I have an iPhone application and I need to implement the following method: +(UITextView *)textView:(UITextView *) withCuttedRect:(CGRect)r This method must cut (fill with [UIColor clearColor]) the rect r from UITextView and return UITextView…
user1385666
  • 357
  • 1
  • 7
  • 17
6
votes
1 answer

AVCaptureSession fails when returning from background

I have a camera preview window which is working well 90% of the time. Sometimes however, when returning to my app if it's been in the background, the preview will not display. This is the code I call when the view loads: - (void) startCamera…
mrEmpty
  • 841
  • 4
  • 19
  • 36
6
votes
3 answers

UITapGestureRecognizer does not respond to subview area outside parent view

I have a UIView called view1. view1 has a subview called subview. I added UITapGestureRecognizer to subview as follow: UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; [subview…
user523234
  • 14,323
  • 10
  • 62
  • 102
6
votes
2 answers

ios - Pinch/zoom from current scale

The following code correctly pinches/zooms the container view, but only after it jumps to a scale of 1.0. How can I modify it so that the container view scales from it's current scale? UIPinchGestureRecognizer *twoFingerPinch =…
soleil
  • 12,133
  • 33
  • 112
  • 183
6
votes
3 answers

On iOS, why does height of a view's frame remain almost the same after rotation?

In ViewController.m, on an iPad, if we print out the view's frame height in a tap event handler: NSLog(@"Height of main view is %f", self.view.frame.size.height); then in Portrait mode, the value is 1004 (20 pixel for device status line, so 1024 -…
Jeremy L
  • 3,770
  • 6
  • 41
  • 62
6
votes
4 answers

iOS animating a view including subviews

I'm trying to make an animation for collapsing a view which includes some subviews. [UIView beginAnimations:@"advancedAnimations" context:nil]; [UIView setAnimationDuration:3.0]; CGRect aFrame = aView.frame; aView.size.height = 0; aView.frame =…
toppless
  • 411
  • 1
  • 6
  • 16
6
votes
4 answers

How to get device orientation via UIView reference?

I need to get device orientation from a ViewController. I cannot base on: [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; because it sometimes…
thelion
  • 93
  • 1
  • 4
6
votes
3 answers

How to switch views with animation - objective c

I have a viewController that has a tableView and a mapView, and only one is visible. I also have a toolbar with segment control with two buttons (list and map) How do I switch between the table view and the map view ? and it's important that the…
Eyal
  • 10,777
  • 18
  • 78
  • 130
6
votes
2 answers

How to set a gradient border on UIView?

It's very easy to put a simple border on a UIView. You simply link to QuartzCore, import it, and use: self.view.layer.borderColor = [UIColor redColor].CGColor; self.view.layer.borderWidth = 2.0f; My question is... Is there a way to make this border…
shawnwall
  • 4,549
  • 1
  • 27
  • 38
6
votes
2 answers

How to move uiview on circle trajectory?

Is there any way to animate uiview moving with cicle trajectory?
B.S.
  • 21,660
  • 14
  • 87
  • 109
5
votes
1 answer

How to retain or animate subview background color in UITableViewCell on selection?

I have a UIView as a subview of a custom subclass of UITableViewCell. I'm using it to change the color of just a small area of the cell dynamically. When I select the cell, the background color of the entire cell -- including the background color…
Ian Terrell
  • 10,667
  • 11
  • 45
  • 66
5
votes
2 answers

iOS: how to stop text animation in partial curl

I'm using a partial curl modal in my iOS application. Please see this video: http://vimeo.com/38643030 During the partial curl transition, the text in the round rect buttons is moving too. How can I stop this?
dhrm
  • 14,335
  • 34
  • 117
  • 183
5
votes
3 answers

touches methods not getting called on UIView placed inside a UIScrollView

I have a Custom Scroll View, subclassing UIScrollView. I have added a scroll view in my viewcontroller nib file and changed its class to CustomScrollView. Now, this custom scroll view (made from xib) is added as a subview on self.view. In this…
anshul
  • 846
  • 1
  • 14
  • 32
5
votes
5 answers

UIImageView fill in UIView

I have a UIImageView as a subview of UIView (frame size 300 x 300). My UIImage would be either portrait, landscape or some odd sizes. How do I fill in the UIImage within the UIView frame size without stretching the image to 300 x 300? The purpose of…
RockBaby
  • 381
  • 1
  • 6
  • 17
5
votes
3 answers

iPhone autoresizingmasks

I've been experiencing problems with designing views so that the subviews behave the way I want when using autoresizingmasks (for example, if the status bar size changes, when using the phone as a hotspot etc.). Is there any good documentation that…
mbord
  • 63
  • 1
  • 3
1 2 3
99
100