[UIView new] // Get a view object allocated and initialized
[[UIView alloc] initWithFrame:(Pass CGRect)] // Get the view allocated and initialized with a frame
[[UIView alloc] init] // Get a view object allocated and initialized
UIView() // Creates an UIView instance with CGRect.zero frame
UIView(frame: CGRect) // Creates an UIView instance specifying the frame
UIView.addSubview(UIView) // add an another UIView instance as subview
UIView.hidden // Get or set the visibility of the view
UIView.alpha // Get or set the view's opacity
UIView.setNeedsLayout() // Forces the view to update its layout
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.
Animating a UIView
Create a UIView
Create UIView using Autolayout
Function to add view with fixed height using autolayout constraints
Function add full resize constraint for created UIView.
Make the view rounded
To make a rounded UIView, specify a cornerRadius for the view's layer.
This also applies any class which inherits from UIView, such as UIImageView.
Here is the result, showing the rounded view effect using the specified corner radius:
To do this you need to include the QuartzCore framework.
A rounded view effect can also be achieved non-programmatically by setting the corresponding properties in Storyboard.
Since layer properties aren't exposed in Storyboard, you have to modify the cornerRadius attribute via the User Defined Runtime Attributes section.
You can use this handy extension to apply rounded view as long as it has same width and height.
To use it:
Programmatically manage UIView insertion and deletion into and from another UIView
Suppose you have a parentView into which you want to insert a new subView programmatically (eg. when you want to insert an UIImageView into a UIViewController's view), than you can do it as below.
You can also add the subView below another subView2, which is already a sub view of parentView using the following code:
If you want to insert it above subView2 you can do it this way:
If somewhere in your code you need to bring a certain subView to front, so above all the others parentView's subviews, you can do it like this:
Finally, if you want to remove subView from parentView, you can do as below:
Shake a View
This function can be used to draw attention to a specific view by shaking it a bit.
Taking a snapshot
You can take a snapshot from a UIView like this:
UIView extension for size and frame attributes
If we want to get the x-cordinate of origin of the view, then we need to write like:
For width, we need to write:
But if we add a simple extension to an UIView, we can get all the attributes very simply, like:
It will also help setting these attributes like:
And the simple extension would be:
We need to add this class file in a project and it'll be available to use throughout the project!
Using IBInspectable and IBDesignable
One (or two) of the coolest new features in recent Xcode releases are the IBInspectable properties and IBDesignableUIViews. These have nothing to do with the functionality of your application but instead impact the developer experience in Xcode. The goal is to be able to visually inspect custom views in your iOS application without running it. So assume that you have a custom view creatively named CustomView that inherits from UIView. In this custom view, it will display a string of text with a designated color. You can also choose not to display any text. We'll need three properties:
We can then override the drawRect function in the class:
Assuming that the text property is set, this will draw a string in the upper left hand corner of the view when the application is run. The problem is we won't know what it looks like without running the application. This is where IBInspectable and IBDesignable come in. IBInspectable allows us to visually set property values of the view in Xcode, just like with the built in controls. IBDesignable will show us a visual preview in the storyboard. Here is how the class should look:
Or in Objective C:
The next screenshots show what happens in Xcode. The first one is what happens after adding the revised class. Notice that there are three new UI elements for the three properties. The Text Color will display a color picker, Text is just an input box and Show Text will give us the options for Off and On which are false and true respectively.
The next is after changing the Text Color to red using the color picker. Also, some text has been provided to make the drawRect function display it. Notice that the view in Interface Builder has been updated as well.
Finally, setting Show Text to Off in the property inspector makes the text display in Interface Builder disappear.
However, We all come up situation when we need to create rounded UIView at multiple views in your Storyboard.Instead of declaring IBDesignable to every views of Storyboard, its better to create an Extension of UIView and get a user interface built just for your every UIView through out the project to create rounded view by setting corner radius.A configurable border radius on any UIView you create in storyboard.
Utilizing Intrinsic Content Size
When creating a UIView subclass, intrinsic content size helps to avoid setting hardcoded height and width constraints
a basic glimpse into how a class can utilize this
If you only want to provide one size intrinsically, you can provide the value UIViewNoIntrinsicMetric for the value that you wish to ignore.
Benefits when using with AutoLayout and Interface Builder
One could take this ImageView (or UIImageView) and set the horizontal alignment to superview center X and the vertical alignment to superview center Y.
Interface builder will complain to you at this point giving the following warning:
This is where Placeholder Intrinsic Size comes in.
Going into the Size inspector panel, and down to the Intrinsic Size dropdown, you can switch this value from Default to Placeholder.
and now interface builder will remove the previous warnings and you can use this size to have dynamically sized views laid out in interface builder.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0