ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP4126Views Subviews Creatinโ€ฆ
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP4126

    Mobile Application Development 2

    Progress0 / 38 done
    Topics38
    1Understanding Xcode2Using the Xcode interface builder and objects library3Understanding view hierarchy and creating a custom app icon
    4Understanding outlets and actions5Using text fields, buttons, labels, web views, and page controllers6Using views with subviews and creating views using code
    7Working with the single view template8Exploring the app delegate and adding new view controllers9Transitioning between multiple view controllers using animations
    10Tabbar and master detail templates
    11Customizing for different inputs12Adjusting text field behaviors and dismissing the keyboard13Detecting keyboard activities with notification center14Using scroll view and responding to keyboard activities programmatically
    15Detecting device hardware16Dynamically adjusting graphical layouts and creating universal apps
    17Understanding UITableView and UITableViewCell18Working with UITableView data source and delegate19Master detail template, drill-down menus, and navigation20Using property lists for data persistence and creating multi-section tables
    21Portrait and landscape modes22Handling device rotation and forcing specific orientation23Dynamically adjusting layouts based on rotation
    24Importing sqlite3 and creating a database25Writing tables, inserting records, and bundling a database with your app26Checking for database existence and reading/displaying data
    27NSTimer class and object transformations28Rotation, scaling, translation, animating image arrays, and playing video
    29Email, Safari, and SMS30Working with camera and photo library
    31Consuming and parsing XML and JSON32Integrating Twitter and Facebook with iOS apps
    33MapKit and MKMapView34Getting and displaying user location and directional information35Displaying map annotations, disclosure buttons, and reverse geocoding36Working with iCloud
    37Gyroscope and accelerometer38Outputting sensor data and using the Shake API
    4 min read
    Topic 6 of 38
    Topic Study Note Illustration
    06

    Using views with subviews and creating views using code

    parentView.addSubview(childView) ๐Ÿ”น Example let parentView = UIView (frame: CGRect (x: 0 , y: 0 , width: 300 , height: 300 )) parentView.backgroundColor = .lightGray let childView =...

    ๐Ÿ“ฑ Using Views with Subviews & Creating Views Using Code (iOS โ€“ Xcode)


    โœ… 1. Definition

    • View: A rectangular UI element displayed on screen (e.g., label, button).
    • Subview: A view that is placed inside another view (parent view).
    • Creating views using code: Instead of using storyboard, UI elements are created and configured programmatically in Swift.

    ๐Ÿง  2. Key Concepts

    ๐Ÿ”น View Hierarchy

    • Views are arranged in a parentโ€“child structure
    • Parent = Superview
    • Child = Subview

    ๐Ÿ”น Programmatic UI

    • UI is built using Swift code
    • No drag-and-drop needed
    • Gives more control and flexibility

    ๐Ÿ”น Frame vs Constraints

    Term Meaning
    Frame Position and size (x, y, width, height)
    Auto Layout Constraints for responsive design

    ๐Ÿ—๏ธ 3. Using Views with Subviews


    ๐Ÿ”น Adding a Subview

    Syntax:

    parentView.addSubview(childView)
    

    ๐Ÿ”น Example

    let parentView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
    parentView.backgroundColor = .lightGray
    
    let childView = UIView(frame: CGRect(x: 50, y: 50, width: 200, height: 200))
    childView.backgroundColor = .blue
    
    parentView.addSubview(childView)
    

    ๐Ÿ‘‰ Result:

    • Blue view appears inside gray view

    ๐Ÿ“Š Diagram Description (for Exams)

    Draw:

    • Large rectangle (Parent View)
    • Smaller rectangle inside it (Subview)
    • Label them clearly

    ๐Ÿ“Œ Important Rules

    • Subview position is relative to parent view
    • Removing parent removes all subviews
    • Subviews are drawn on top of parent
    • Order matters (top view overlaps below views)

    โš™๏ธ 4. Creating Views Using Code


    ๐Ÿ”น Step 1: Import UIKit

    import UIKit
    

    ๐Ÿ”น Step 2: Create a View

    let myView = UIView()
    myView.backgroundColor = .red
    

    ๐Ÿ”น Step 3: Set Frame

    myView.frame = CGRect(x: 50, y: 100, width: 200, height: 100)
    

    ๐Ÿ”น Step 4: Add to Main View

    self.view.addSubview(myView)
    

    ๐Ÿงฉ 5. Creating UI Elements Programmatically


    ๐Ÿ”น Label Example

    let label = UILabel()
    label.text = "Hello World"
    label.frame = CGRect(x: 50, y: 100, width: 200, height: 50)
    
    self.view.addSubview(label)
    

    ๐Ÿ”น Button Example

    let button = UIButton(type: .system)
    button.setTitle("Click Me", for: .normal)
    button.frame = CGRect(x: 50, y: 200, width: 150, height: 50)
    
    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    
    self.view.addSubview(button)
    
    @objc func buttonTapped() {
        print("Button Pressed")
    }
    

    ๐Ÿ“ 6. Using Auto Layout in Code

    Instead of frame:

    myView.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        myView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        myView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        myView.widthAnchor.constraint(equalToConstant: 100),
        myView.heightAnchor.constraint(equalToConstant: 100)
    ])
    

    ๐Ÿ’ก 7. Example

    ๐ŸŽฏ Colored Box App

    • Create a red box inside main view
    • Add a label inside the box
    let box = UIView(frame: CGRect(x: 50, y: 100, width: 200, height: 200))
    box.backgroundColor = .red
    
    let label = UILabel(frame: CGRect(x: 20, y: 80, width: 160, height: 40))
    label.text = "Inside Box"
    label.textAlignment = .center
    
    box.addSubview(label)
    self.view.addSubview(box)
    

    โš ๏ธ 8. Common Mistakes

    • โŒ Forgetting to addSubview โ†’ view not visible
    • โŒ Wrong frame values โ†’ view off screen
    • โŒ Missing constraints โ†’ layout issues
    • โŒ Not setting translatesAutoresizingMaskIntoConstraints = false

    ๐Ÿ“ 9. Likely Exam Questions

    1. Define view and subview.
    2. Explain view hierarchy with example.
    3. How do you add a subview in iOS?
    4. Write code to create a UIView programmatically.
    5. Differentiate between frame and Auto Layout.
    6. Create a button using code and handle its action.
    7. Explain advantages of programmatic UI.
    8. Draw a diagram of view and subview relationship.

    ๐Ÿ“š 10. Quick Revision Summary

    • View = UI element

    • Subview = Child inside another view

    • Use addSubview() to attach views

    • UI can be created using:

      • Storyboard OR Code
    • Programmatic UI gives:

      • More control
      • Better customization
    • Use:

      • Frame โ†’ fixed layout
      • Auto Layout โ†’ responsive design

    โ† Previous topic 5
    Using text fields, buttons, labels, web views, and page controllers
    Next topic 7 โ†’
    Using View Controllers: Working with the single view template

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    596words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Mobile Application Development 2