Close Menu
  • Home
  • AI
  • Big Data
  • Cloud Computing
  • iOS Development
  • IoT
  • IT/ Cybersecurity
  • Tech
    • Nanotechnology
    • Green Technology
    • Apple
    • Software Development
    • Software Engineering

Subscribe to Updates

Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

    What's Hot

    Gesture Recognition for Busy Hands

    October 13, 2025

    Inside the ‘Let’s Break It Down’ Series for Network Newbies

    October 13, 2025

    SVS Engineers: Who are the people that test-drive your network?

    October 12, 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Big Tee Tech Hub
    • Home
    • AI
    • Big Data
    • Cloud Computing
    • iOS Development
    • IoT
    • IT/ Cybersecurity
    • Tech
      • Nanotechnology
      • Green Technology
      • Apple
      • Software Development
      • Software Engineering
    Big Tee Tech Hub
    Home»iOS Development»ios – How to make UIView fit the size of its contents?
    iOS Development

    ios – How to make UIView fit the size of its contents?

    big tee tech hubBy big tee tech hubSeptember 8, 2025002 Mins Read
    Share Facebook Twitter Pinterest Copy Link LinkedIn Tumblr Email Telegram WhatsApp
    Follow Us
    Google News Flipboard
    ios – How to make UIView fit the size of its contents?
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link


    Make the height implied by its contents: remove the fixed height, and fully constrain the content from top → bottom inside innerView. Also don’t fix titleLbl’s height.

    func setupUI() {
        view.backgroundColor = .white
    
        // Title
        let titleLbl = UILabel()
        titleLbl.text = book.title
        titleLbl.textColor = .black
        titleLbl.textAlignment = .center
        titleLbl.numberOfLines = 0
        titleLbl.lineBreakMode = .byWordWrapping
        titleLbl.font = UIFont(name: "Inter-Regular", size: 16)
    
        titleLbl.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(titleLbl)
    
        NSLayoutConstraint.activate([
            titleLbl.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20),
            titleLbl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            titleLbl.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
            // ❌ no fixed height — let it wrap
        ])
    
        // Close button (use constraints instead of frames)
        let backBtn = UIButton(type: .system)
        backBtn.setImage(UIImage(named: "close-circle"), for: .normal)
        backBtn.tintColor = .black
        backBtn.backgroundColor = .clear
        backBtn.addTarget(self, action: #selector(goback), for: .touchUpInside)
        backBtn.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(backBtn)
    
        NSLayoutConstraint.activate([
            backBtn.centerYAnchor.constraint(equalTo: titleLbl.centerYAnchor),
            backBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            backBtn.widthAnchor.constraint(equalToConstant: 40),
            backBtn.heightAnchor.constraint(equalToConstant: 40)
        ])
    
        // Container
        let innerView = UIView()
        self.innerView = innerView
        innerView.backgroundColor = UIColor(red: 0.92, green: 0.96, blue: 0.99, alpha: 1.0)
        innerView.layer.cornerRadius = 12
        innerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(innerView)
    
        NSLayoutConstraint.activate([
            innerView.topAnchor.constraint(equalTo: titleLbl.bottomAnchor, constant: 24),
            innerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            innerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20)
            // ❌ no fixed height here
        ])
    
        // Body label
        let quote = book.quotes![currentIndex]
        let bodyLbl = UILabel()
        bodyLbl.text = quote
        bodyLbl.textColor = .black
        bodyLbl.textAlignment = .center
        bodyLbl.numberOfLines = 0
        bodyLbl.lineBreakMode = .byWordWrapping
        bodyLbl.font = UIFont(name: "Inter-Regular", size: 16)
        bodyLbl.translatesAutoresizingMaskIntoConstraints = false
        innerView.addSubview(bodyLbl)
    
        NSLayoutConstraint.activate([
            bodyLbl.topAnchor.constraint(equalTo: innerView.topAnchor, constant: 20),
            bodyLbl.leadingAnchor.constraint(equalTo: innerView.leadingAnchor, constant: 12),
            bodyLbl.trailingAnchor.constraint(equalTo: innerView.trailingAnchor, constant: -12)
        ])
    
        // Share button
        let shareButton = UIButton(type: .system)
        shareButton.setTitle("Share", for: .normal)
        shareButton.setTitleColor(.white, for: .normal)
        shareButton.backgroundColor = .black
        shareButton.addTarget(self, action: #selector(self.share), for: .touchUpInside)
        shareButton.layer.cornerRadius = 12
        shareButton.clipsToBounds = true
        shareButton.translatesAutoresizingMaskIntoConstraints = false
        innerView.addSubview(shareButton)
    
        NSLayoutConstraint.activate([
            shareButton.centerXAnchor.constraint(equalTo: innerView.centerXAnchor),
            shareButton.topAnchor.constraint(equalTo: bodyLbl.bottomAnchor, constant: 24),
            shareButton.heightAnchor.constraint(equalToConstant: 36),
            shareButton.widthAnchor.constraint(equalToConstant: 150),
    
            // 👇 This is the key: pin the last subview to the bottom
            innerView.bottomAnchor.constraint(equalTo: shareButton.bottomAnchor, constant: 20)
        ])
    
        // Next button visibility (logic bug fix)
        let count = book.quotes!.count
        nextBtn.isHidden = (currentIndex + 1) >= count
    }
    



    Source link

    contents fit iOS size UIView
    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Copy Link
    tonirufai
    big tee tech hub
    • Website

    Related Posts

    ios – Apple mapkit route function dose not works in China

    October 12, 2025

    swift – Does UIDevice.current.identifierForVendor change after iCloud backup and restore on another iOS device?

    October 11, 2025

    uitabbarcontroller – How to add custom UIView to floating UITabBarItem in iOS 26 Liquid Glass UITabBar

    October 10, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Editors Picks

    Gesture Recognition for Busy Hands

    October 13, 2025

    Inside the ‘Let’s Break It Down’ Series for Network Newbies

    October 13, 2025

    SVS Engineers: Who are the people that test-drive your network?

    October 12, 2025

    macOS Sequoia (version 15) is now available for your Mac with some big upgrades

    October 12, 2025
    Advertisement
    About Us
    About Us

    Welcome To big tee tech hub. Big tee tech hub is a Professional seo tools Platform. Here we will provide you only interesting content, which you will like very much. We’re dedicated to providing you the best of seo tools, with a focus on dependability and tools. We’re working to turn our passion for seo tools into a booming online website. We hope you enjoy our seo tools as much as we enjoy offering them to you.

    Don't Miss!

    Gesture Recognition for Busy Hands

    October 13, 2025

    Inside the ‘Let’s Break It Down’ Series for Network Newbies

    October 13, 2025

    Subscribe to Updates

    Get the latest technology news from Bigteetechhub about IT, Cybersecurity and Big Data.

      • About Us
      • Contact Us
      • Disclaimer
      • Privacy Policy
      • Terms and Conditions
      © 2025 bigteetechhub.All Right Reserved

      Type above and press Enter to search. Press Esc to cancel.