Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Core Properties and Manipulation Methods of UIView in iOS

Tech 1
// 1. Transparency control
// Values range from 0.0 (fully transparent) to 1.0 (fully opaque)
view.alpha = 0.5

// 2. Clipping behavior
// When enabled, content outside the view's bounds is not rendered
view.clipsToBounds = true

// 3. Visibility toggle
// Hides or shows the entire view without affecting layout
view.isHidden = true

// 4. Interaction enablement
// Disabling prevents touch events, gesture recognizers, and hit testing
view.isUserInteractionEnabled = false

// 5. Identifier tag
// Integer value for runtime identification (e.g., in delegate callbacks)
view.tag = 1001

// 6. Exclusive touch handling
// Ensures only this view receives all touches during a single gesture sequence
view.isExclusiveTouch = true

// 7. Frame rectangle
// Position and size relative to the superview's coordinate system
view.frame = CGRect(x: 20, y: 40, width: 100, height: 60)

// 8. Center point
// Adjusts position by moving the view’s center coordinate
view.center = CGPoint(x: 150, y: 200)

// 9. Bounds rectangle
// Defines internal coordinate space; origin is always (0, 0), size affects rendering area
view.bounds = CGRect(origin: .zero, size: CGSize(width: 120, height: 80))

// 10. Transform matrix
// Applies affine transformations like scaling and rotation
view.transform = CGAffineTransform(scaleX: 1.2, y: 0.8)
view.transform = CGAffineTransform(rotationAngle: .pi / 4)
view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5).rotated(by: -.pi / 6)

// 11. Superview reference
// Read-only access to parent container
if let parent = view.superview {
    print("Parent view exists")
}

// 12. Window reference
// Read-only access to the hosting UIWindow instance
if let window = view.window {
    print("View is attached to a window")
}

// Note: superview and window are nil in viewDidLoad because the view hierarchy isn't yet added to the window.
// They become available after viewDidAppear(_:) is called.

// 13. Subview auto-resizing policy
// Controls whether child views automatically adjust when the parent resizes
view.autoresizesSubviews = false

// 14. Autoresizing mask
// Specifies how the view adjusts its frame when its superview changes size
view.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]

// 15. Content display mode
// Determines how content (e.g., images) fits within the bounds
view.contentMode = .scaleAspectFit
// Other options:
// .scaleToFill, .scaleAspectFill, .redraw, .center, .top, .bottom, etc.

// 16. Background color
// Sets the fill color behind subviews
view.backgroundColor = UIColor.systemBlue.withAlphaComponent(0.7)

View Hierarchy Managemant Methods

// Add a new subview
containerView.addSubview(childView)

// Reorder subviews
containerView.bringSubviewToFront(anotherView)
containerView.sendSubviewToBack(backgroundView)

// Remove from hierarchy
childView.removeFromSuperview()

// Insert at specific index
containerView.insertSubview(newView, at: 2)

// Insert relative to another view
containerView.insertSubview(newView, aboveSubview: existingView)
containerView.insertSubview(newView, belowSubview: existingView)

// Swap positions of two subviews by index
containerView.exchangeSubview(at: 0, withSubviewAt: 3)

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.