<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Xiaoteng Han on Medium]]></title>
        <description><![CDATA[Stories by Xiaoteng Han on Medium]]></description>
        <link>https://medium.com/@xiaotenghan?source=rss-c09884b99ad6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*exQ0dHC0LRuP10iO.</url>
            <title>Stories by Xiaoteng Han on Medium</title>
            <link>https://medium.com/@xiaotenghan?source=rss-c09884b99ad6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 13:48:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xiaotenghan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to hide a view in SwiftUI]]></title>
            <link>https://medium.com/@xiaotenghan/how-to-hide-a-view-in-swiftui-c9a93106a8bd?source=rss-c09884b99ad6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9a93106a8bd</guid>
            <dc:creator><![CDATA[Xiaoteng Han]]></dc:creator>
            <pubDate>Fri, 07 Mar 2025 12:19:06 GMT</pubDate>
            <atom:updated>2025-03-07T12:29:45.371Z</atom:updated>
            <content:encoded><![CDATA[<p>Hiding a view in SwiftUI might seem like a simple task, but there are multiple ways to achieve it, each with its own advantages and trade-offs. In this article, we’ll explore the different methods to hide a view in SwiftUI, focusing on 3 main aspects: <strong>Readability</strong>, <strong>Accessibility</strong> and <strong>Animation</strong>, and help you decide which approach is best for your specific scenario.</p><h3>Old school, conditionally add the view</h3><p>One of the most straightforward ways to hide a view is to conditionally include or exclude it from the view hierarchy. This approach is useful when you want to completely remove the view from the layout when it’s not needed.</p><pre>struct ContentView: View {<br>    @State private var isViewVisible = true<br>    var body: some View {<br>        VStack {<br>            if isViewVisible {<br>                Text(&quot;Hello, World!&quot;)<br>            }<br>            Button(&quot;Toggle View&quot;) {<br>                isViewVisible.toggle()<br>            }<br>        }<br>    }<br>}</pre><h4>Pros:</h4><h4>1. Readability</h4><ul><li><strong>Clear Intent</strong>: Conditionally adding a view makes the code’s intent explicit. It’s easy to understand that the view is only present when the condition is met.</li><li><strong>Simplified Logic</strong>: The logic for showing or hiding the view is contained within the if statement, making it easy to follow.</li></ul><h4>2. Accessibility</h4><ul><li><strong>Removed from Accessibility Tree</strong>: When the view is not added to the hierarchy, it is also removed from the accessibility tree. This ensures that screen readers and other assistive technologies won’t interact with or announce the hidden view, improving the user experience for accessibility users.</li><li><strong>No Hidden Focus Issues</strong>: Since the view is completely removed, there’s no risk of it accidentally receiving focus when it shouldn’t.</li></ul><h4>3. Animation</h4><ul><li><strong>No Built-in Animation</strong>: While conditional view addition doesn’t natively support animations, you can still animate the transition using SwiftUI’s withAnimation or transition APIs. For example:</li></ul><pre>Button(&quot;Toggle View&quot;) {<br>    withAnimation(.easeInOut) {<br>        isViewVisible.toggle()<br>    }<br>}</pre><h4>Cons:</h4><h4>1. Readability</h4><ul><li><strong>Potential Code Duplication</strong>: If the condition is used in multiple places, it can lead to repetitive code. For example, if you need to show/hide the same view in different parts of the UI, you might end up duplicating the if statement.</li><li><strong>Related Views May Shift Position</strong>: When a view is conditionally added or removed, the surrounding views may move to fill the empty space. This can lead to layout instability and require extra logic to handle placeholders or maintain consistent spacing. For example:</li></ul><pre>VStack {<br>    if isViewVisible {<br>        Text(&quot;Middle View&quot;)<br>    } else {<br>        Spacer().frame(height: 30) // Placeholder to maintain space<br>    }<br>    <br>    Button(&quot;Toggle View&quot;) {<br>        isViewVisible.toggle()<br>    }<br>}</pre><ul><li><strong>Complex Conditions</strong>: If the condition for showing/hiding the view is complex, it can make the code harder to read. For example:</li></ul><pre>if isViewVisible &amp;&amp; !isLoading &amp;&amp; !isError {<br>    Text(&quot;Hello, World!&quot;)<br>}</pre><h4>2. Accessibility</h4><ul><li><strong>State Loss</strong>: Since the view is removed from the hierarchy, any state or user input within the view will be lost when it’s hidden. This can be problematic if the view contains a form or user input that needs to persist.</li></ul><h4>3. Animation</h4><ul><li><strong>No Smooth Transitions</strong>: Unlike opacity-based hiding(which we will cover next), conditional view addition doesn’t inherently support smooth transitions. You’ll need to explicitly add animations using transition or withAnimation, which can add complexity.</li><li><strong>Performance Overhead</strong>: If the view is complex or expensive to initialise, recreating it every time it reappears can lead to performance issues, especially during animations.</li></ul><h3>How about just change the view’s opacity to 0?</h3><p>This method involves setting a view’s opacity to 0 to make it invisible while keeping it in the view hierarchy. It’s a popular approach because it allows for smooth animations and preserves the view’s state, but it also has some trade-offs, especially regarding accessibility and layout.</p><pre>struct ContentView: View {<br>    @State private var isViewVisible = false    <br>    var body: some View {<br>        VStack {<br>            Text(&quot;Hello, World!&quot;)<br>                .opacity(isViewVisible ? 1 : 0) // Hide by setting opacity to 0<br>            <br>            Button(isViewVisible ? &quot;Hide View&quot; : &quot;Show View&quot;) {<br>                isViewVisible.toggle()<br>            }<br>        }<br>    }<br>}</pre><h4>Pros:</h4><h4>1. Readability</h4><ul><li><strong>No Conditional Logic</strong>: Unlike conditional view addition, there’s no need for if statements or placeholders, which keeps the code concise and readable.</li></ul><h4>2. Accessibility</h4><ul><li>Since the view remains in the hierarchy, it stays in the accessibility tree. It depends on SwiftUI to handle accessibility</li></ul><h4>3. Animation</h4><ul><li><strong>Smooth Transitions</strong>: Opacity-based hiding works seamlessly with SwiftUI’s animation system. You can easily animate the view’s appearance and disappearance with smooth fade effects.</li></ul><pre>.opacity(isViewVisible ? 1 : 0)<br>.animation(.easeInOut(duration: 0.3), value: isViewVisible)</pre><h4>Cons:</h4><h4>1. Readability</h4><ul><li><strong>Potential Clutter</strong>: If you need to combine opacity-based hiding with other modifiers (e.g., accessibility adjustments or layout fixes), the code can become cluttered.</li></ul><pre>Text(&quot;Hello, World!&quot;)<br>    .opacity(isViewVisible ? 1 : 0)<br>    .accessibility(hidden: !isViewVisible)<br>    .animation(.easeInOut, value: isViewVisible)</pre><h4>2. Accessibility</h4><ul><li><strong>Remains in Accessibility Tree</strong>: Even when the view is invisible, it technically remains in the accessibility tree, depending on SwiftUI internal logic to respond to accessibility events, if your scenario is to explicitly hide the accessibility item, consider to use .accessibility(hidden: true). Anyway, this can confuse users relying on assistive technologies as well as developers trying to understand the overall logic.</li></ul><h4>3. Animation</h4><ul><li><strong>Layout Impact</strong>: Unlike conditional view addition, opacity-based hiding doesn’t remove the view from the layout. This means the view still occupies space, even when invisible, which can affect the surrounding layout. I know we just talked about the benefits of keeping the hidden view in view hierarchy — but consider the following scenario</li><li><strong>Example</strong>: If you have a VStack with multiple views and one is hidden using opacity, the hidden view will still take up space, potentially leaving an unwanted gap.</li><li>This is exactly where an experienced developer has to make a wise decision for the situation at hand.</li></ul><h3>But there is a native method to do it, right?</h3><p>Yes, let’s cover it briefly, the hidden() method is from apple’s documentation:</p><p><a href="https://developer.apple.com/documentation/swiftui/view/hidden()">https://developer.apple.com/documentation/swiftui/view/hidden()</a>.</p><p>However, as you can see this modifier accepts no argument which limits its usage. You can use it as below example:</p><pre>struct ContentView: View {<br>    var body: some View {<br>        Text(&quot;Hello, World!&quot;)<br>            .hidden() // The view is hidden but still in the hierarchy<br>    }<br>}</pre><ul><li><strong>Readability</strong>: High, as the modifier is simple and clear, but lacks conditional support.</li><li><strong>Accessibility</strong>: Excellent, as the view is removed from the accessibility tree when hidden.</li><li><strong>Animation</strong>: Requires additional setup but can be achieved with transitions and explicit animations.</li></ul><h3>Conclusion</h3><p>Hiding a view in SwiftUI can be accomplished in multiple ways, each with its own trade-offs. Whether you conditionally add the view, use the hidden() modifier, adjust the opacity, or implement a custom solution, the best approach depends on your specific needs. When making a decision, consider factors like <strong>layout hierarchy</strong>, <strong>animation</strong>, <strong>accessibility</strong>, and <strong>code readability</strong>.</p><p>By understanding the strengths and limitations of each method, you can create more dynamic, efficient, and user-friendly SwiftUI interfaces.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9a93106a8bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How does iOS static framework and dynamic framework work?]]></title>
            <link>https://medium.com/@xiaotenghan/how-does-ios-static-framework-and-dynamic-framework-work-23b355268195?source=rss-c09884b99ad6------2</link>
            <guid isPermaLink="false">https://medium.com/p/23b355268195</guid>
            <category><![CDATA[static-framework]]></category>
            <category><![CDATA[cocoapods]]></category>
            <category><![CDATA[carthage]]></category>
            <category><![CDATA[dynamic-framework]]></category>
            <category><![CDATA[swift-package-manager]]></category>
            <dc:creator><![CDATA[Xiaoteng Han]]></dc:creator>
            <pubDate>Mon, 13 Nov 2023 02:10:48 GMT</pubDate>
            <atom:updated>2023-11-13T02:10:48.026Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>What are they?</strong></p><p>If you have written any iOS app, you’d be already “automatically” using dynamic frameworks provided by the system. Though, we’re only talking about 3rd party frameworks today. As you already know, 3rd party frameworks are helpful for sharing code and reduce the complexity of the codebase. However, once you know a 3rd party framework is helpful, how are you going to import it to your own project? In the context of iOS, there are basically 2 ways, you can import it as static framework or dynamic framework. Here are their definitions:</p><p>A static framework is a collection of binary files(.framework files) that is linked directly into an application at compile time;</p><p>A dynamic framework is a separate binary file(.framework file) that contains precompiled code, resources, and symbols, which can be loaded and used by an application at runtime.</p><p>Since the only difference between the two are linking. Someone may think there is no big deal because linking is almost always automatic in modern days and loading a framework at runtime normally goes real fast that none really notice. Then why bother? Well, to be short, its probably easier and better to leave it to default configuration and automatic process 99% of the time, unless you need to consider app size or startup loading time.</p><p><strong>The flow</strong></p><p>It’s probably easier to explain the difference between Static and Dynamic framework via how they work(in abstract level) from build to run</p><p>If you setup your project to import a static framework and build the project.</p><ol><li>Building: Compiler will start to build your static framework then your source code, producing a bunch of Binary files</li><li>Linking: Linker will then kick-in to link up these object files, both from your static framework and source code and finally produce an App Executable</li><li>Packaging: Packaging tool will collect the App Executable along with other resources like icon, image and other resources into a folder structure and produce the App Bundle.</li><li>Running: When user runs the app, the App Executable eventually get run which means both the static framework and your source code binary objects get load to the memory and get executed.</li></ol><p>Now, in comparison to the Dynamic framework:</p><ol><li>Building: Compiler will still build the dynamic framework then your source code and produce a bunch of Binary files.</li><li>Linking: However, the linker only link up the Binary files from your source code, leaving the Binary files for later processing. Now you may have a question, what about these dependencies in your code that requires the dynamic framework? Don’t worry, linker will mark the name of the required frameworks, assuming they will appear at runtime. So the App Executable is still produced, with assumption that dynamic framework dependencies will be fulfilled at runtime.</li><li>Packaging: Now the packaging tool jumps in, collecting the App Executable along with all required resources. Notice here, “all required resources” include the dynamic framework’s Binary files, the packaging tool will actually put these Binary files into a folder called “frameworks” in the final App Bundle.</li><li>Running: When user runs the app, the OS first tries to load the dynamic framework from the App Bundle into memory, if the dynamic framework is already loaded, then there is no need to load the same thing again. Anyway, the loaded dynamic framework will have a memory address and this memory address is feed to your App Executable. But what if the dynamic framework is not found? Your App will simply crash because of missing dependency.</li></ol><p><strong>Pros and Cons</strong></p><p>Hopefully above explanation makes a bit sense, following we will quickly list a few pro and cons for static and dynamic framework.</p><p><strong>Static Framework:</strong></p><p><strong>Pros:</strong></p><p>No missing dependency</p><p>No need to load framework dependency at runtime</p><p>Larger App Executable size (not really affecting App Bundle size)</p><p><strong>Cons:</strong></p><p>Each app will load it’s own framework into memory, potentially load duplicated frameworks.</p><p><strong>Dynamic Framework:</strong></p><p><strong>Pros:</strong></p><p>Only load required framework, once only. Saving memory footprint.</p><p><strong>Cons:</strong></p><p>Rely on OS to load framework dependencies, potentially get missing dependency.</p><p>Slower on app start up</p><p><strong>Which dependency management tool to use?</strong></p><p>There are mainly 3 dependency managers available today.</p><p>Cocoapods is a centralised dependency manager that is open source and was built with Ruby. It is widely used by iOS developers all the way back to Objective-C days.</p><p>Carthage is a decentralised dependency manager, also open source but was built with Swift. As a newer dependency manager than Cocoapods, Carthage has gathered a lot of attention for avoiding the need of a centralised dependency repo. We will go into more details for the difference.</p><p>Swift Package Manager(SPM) is the Apple’s offical dependency manager and being the newest tool available to developers, it is natively integrated with Xcode and more iOS developer are adopting it everyday.</p><p>There are already too many articles on the internet talking about these dependency managers and we’re not getting into too much of the details. Basically any of the 3 dependency manager will get your daily job done, you’re only picking from slight differences in 3rd party framework variability, project setup difficulty and of course your personal/team preferences.</p><p>At last, there is just one more topic, the overall app build time, which dependency manager will be faster?. Doesn’t matter which dependency manager nor static or dynamic framework, you’re (almost always) pulling in the source code of the 3rd framework which means there must time to build the source code to Binary files. Why? you may wonder, can’t we just import Binary files directly? Because there is no unified form of Binary Object file for all environment. It’s always safer to import the source code and build it under local environment. Anyway, to save on build time, dependency managers require extra time to build the 3rd party framework on initial integration(clean build) to the project and try to cache the build as much as possible. It’s worth to mention that Carthage builds the 3rd party frameworks before integrating them to the project so the framework building is only performed once and is completely separated from project build. So as long as you’re building on your dev laptop, the dependency managers will try the best to save build time for 3rd party frameworks, in the worst case, you’ll get longer build time only after doing a complete clean on Xcode.</p><p>How about the build time on CI? You know CI always start with a completely clean state, in other words, CI is alway on clean build so there is no chance for dependency manager to cache any build nor save build time. So what to do if your CI is spending too long on building 3rd party dependencies? Basically you want to avoid building these 3rd party frameworks at all. If you have prepared to the pre-built binary files(.framework files), directly import these.framework files as static or dynamic frameworks to your project with out help from the dependency manager might worth a try. However, keep in mind that this will involve a lot of manual work and hide lots of dependency details. Only use it on dependencies that actually takes too long and leverage the power of dependency manager for everything else. Lastly, remember to document your changes for future reference.</p><p>Conclusion</p><p>Hopefully this article has helped a bit on understanding the difference between static and dynamic framework as well as their pros and cons. We have also extended the topic to practical issues like dependency managers and build time. I personally find many people mix these topics together but really they’re quite different from each other. Thinking differently? Please leave a comment and we can discuss it further. Otherwise, Thanks for reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23b355268195" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>