SwiftUI

Stacks and Frames in SwiftUI

1. Container Child Limit Container View are limited to 10 direct descendant views. If a stack contains more than 10 View then Xcode automatically throw an error: Extra arguments at positions #11, #12 in call Ex: ...

J
Joynal Abedin
5
Stacks and Frames in SwiftUI

1. Container Child Limit

Container View are limited to 10 direct descendant views. If a stack contains more than 10 View then Xcode automatically throw an error: Extra arguments at positions #11, #12 in call Ex:
//
//  ContentView.swift
//  Container Child Limit
//
//  Created by JOYNAL ABEDIN on 17/9/22.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
        }
    }
}
Output: it throw an error because of more than 10 view. So if we want to solve this problem than the views will need to be embedded into multiple containers. For this containers we can use Group View Ex:
//
//  ContentView.swift
//  Container Child Limit
//
//  Created by JOYNAL ABEDIN on 17/9/22.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack{
            Group {
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
            }
            Group {
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
                Text(\"Static Text\")
            }
           
        }
    }
}

2. Text Line Limits and Layout Priority

By default, the HStack will display the text within its Text View Children on a single line. Take for example the following HStack views are taken an Image and two children text views:
//
//  ContentView.swift
//  Container Child Limit
//
//  Created by JOYNAL ABEDIN on 17/9/22.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        //Text line limit
        HStack {
            Image(systemName: \"airplane\")
            Text(\"Flight Times:\")
            Text(\"London\")
        }.font(.largeTitle)
    }
}
Output:
For small device:
\"page168image5111808\"
for extra large device
\"page168image5112432\"
If the stacks didn't fillup the text one line then it will take another line. It will automatically wrap into multiple line it necessary.
But we can limitation the text line using .lineLimi() . Below example:
//
//  ContentView.swift
//  Container Child Limit
//
//  Created by JOYNAL ABEDIN on 17/9/22.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        //Text line limit
        HStack {
            Image(systemName: \"airplane\")
            Text(\"Flight Times:\")
            Text(\"London\")
        }
        .font(.largeTitle)
        .lineLimit(1)
    }
}
Output:
For small device:
\"page168image5111808\"
for extra large device:
\"\"
Then we set the priority modifier of the text. If we needs to important show full text based on space then we set the priority of the text. Suppose, we need to show \"London\" Text then we should write the code below:
//
//  ContentView.swift
//  Container Child Limit
//
//  Created by JOYNAL ABEDIN on 17/9/22.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        //Text line limit
        HStack {
            Image(systemName: \"airplane\")
            Text(\"Flight Times:\")
            Text(\"London\")
                .layoutPriority(1)
        }
        .font(.largeTitle)
        .lineLimit(1)
    }
}
output:
\"page169image5185248\"
J

Written by Joynal Abedin

Passionate about technology, code, and sharing knowledge.

0 Comments

Leave a Comment