SwiftUI isn’t perfect yet, and there are some flaws in practical use. Online tutorials are few and far between. Therefore specially collates some peacetime development to encounter the problem, provides free to the reader.

(Note: This article is intended for SwiftUI readers who have some basic knowledge.)

Adjust the StatusBarStyle StatusBarStyle

Try the Info. The plist and UIApplication. StatusBarStyle method is invalid. If you have UIViewController as the root view, override the preferredStatusBarStyle method to control the global view. If you want to style a single page, use preferredColorScheme(.light), but the test doesn’t seem to work. There is another method: stackoverflow.com/questions/5…

Adjust NavigationBar style NavigationBar

Let naviAppearance = UINavigationBarAppearance () naviAppearance. ConfigureWithOpaqueBackground () / / no transparent background style NaviAppearance. BackgroundColor = UIColor. WhiteColor / / background naviAppearance shadowColor = UIColor. WhiteColor / / shadow color NaviAppearance. TitleTextAttributes = [:] / / heading styles naviAppearance. LargeTitleTextAttributes = [:] / / headline style UINavigationBar.appearance().standardAppearance = naviAppearance UINavigationBar.appearance().compactAppearance = naviAppearance UINavigationBar.appearance().scrollEdgeAppearance = naviAppearanceCopy the code

Note configureWithOpaqueBackground () needs to call before other attribute set, in addition to transparent background configureWithTransparentBackground (), Setting the background blur effect backgroundEffect(), background and shadow images, and navigation bar button styles can also be modified.

Adjust the TabBar style TabBar

Let itemAppearance = UITabBarItemAppearance () itemAppearance. Normal. IconColor = UIColor. WhiteColor / / normal icon color ItemAppearance. Normal. TitleTextAttributes = [:] / / normal text style itemAppearance. Selected. IconColor = UIColor. WhiteColor / / Selected icon color itemAppearance. Selected. TitleTextAttributes = [:] / / selected text style let tabBarAppearance = UITabBarAppearance () TabBarAppearance. ConfigureWithOpaqueBackground () / / opaque background style tabBarAppearance stackedLayoutAppearance = itemAppearance TabBarAppearance. BackgroundColor = UIColor. WhiteColor / / background tabBarAppearance shadowColor = UIColor. The clear / / shadow color UITabBar.appearance().standardAppearance = tabBarAppearanceCopy the code

Note configureWithOpaqueBackground () also need to call before other attribute set, and have the same Settings as UINavigationBarAppearance, besides appearance can also set indicator for each item labels.

TabView

Set the default selected page: the method is as follows, and each tag item needs to set the index value tag().

TabView(selection: $selectIndex, content: {})
Copy the code

Control the bottom TAB bar to show and hide:

UITabBar.appearance().isHidden = true
Copy the code

NavigationView/TabView/TabBar/TabBar/TabBar/TabBar/TabBar/TabBar/TabBar

The keyboard

The focused() method has been added to iOS15. Note that this method does not work on view initialization and requires onAppear() to be called after a certain amount of time. Before this system can only custom control method to realize reference this: stackoverflow.com/questions/5…

To close the keyboard, either way:

UIApplication.shared.keyWindow? .endEditing(true) UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)Copy the code

Add keyboard toolbar:

.toolbar()
Copy the code

gestures

To get pressed and released:

.simultaneousGesture(
    DragGesture(minimumDistance: 0)
        .onChanged({ _ in })
        .onEnded({ _ in })
)
Copy the code

ScrollView to the specified position with code: ScrollViewReader gets the location, and in onAppear() set the location scrollTo(), which we’re actually using to discover that we need to delay the execution to be effective. We can put the execution in dispatchqueue.main.async ().

TextEditor

Modify background color:

UITextView.appearance().backgroundColor
Copy the code

Handling the Return key to end editing:

.onChange(of: text) { value in if value.last == "\n" { UIApplication.shared.keyWindow? .endEditing(true) } }Copy the code

Text Indicates the internal alignment of the Text

multilineTextAlignment(.center)
Copy the code

Page jump

Error prone: It is common in development to select an item from a list and go to a sub-page. Click the button to return to the previous page. If you click on an item in the list again, you will see that the page content is incorrect. If you are using NavigationLink to jump to the page and pass the isActive parameter, you will run into this problem. The reason is that multiple pages use the same isActive parameter. The solution is to control each item in the list with a separate variable. NavigationView should also not be written outside the TabView, which can cause confusing problems.

Initialization of the property wrapper in init

Init (); init (); init ();

_value = State<Int>(initialValue: 1) _value = Binding<Bool>.Copy the code

How does a View ignore touch events

allowsHitTesting(false)
Copy the code