小编Pet*_*ert的帖子

xcode 11 beta中的新导航栏行为是错误还是故意的?

在Xcode 11 beta中编译了我的一个应用程序后,我注意到导航栏在prefersLargeTitles设置时没有背景。这是预期的行为吗?

我注意到这是向下滚动时消息应用程序现在的工作方式,可见一个大标题,没有导航栏背景。

这是用于设置navBar属性的代码:

 override func viewWillAppear(_ animated: Bool) {
    let textAttributes = [NSAttributedString.Key.foregroundColor:ThemeManager.shared.default1]
    self.navigationController?.navigationBar.largeTitleTextAttributes = textAttributes
    self.navigationController?.navigationBar.titleTextAttributes = textAttributes
    self.navigationController?.navigationBar.tintColor = ThemeManager.shared.default1
 self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.navigationBar.prefersLargeTitles = true
    let nav = self.navigationItem
    nav.title = "My Profile"
}
Run Code Online (Sandbox Code Playgroud)

以下是一些显示差异的图像:

左,在Xcode 10上编译,右,在Xcode 11 beta上:

在此处输入图片说明 在此处输入图片说明

向上滚动至11 Beta版本后,背景会逐渐消失。请注意,未在Xcode 11 beta中编译的应用仍将以正常方式运行,仅在出于某些原因进行编译后才会更改。这是故意的,我将如何恢复原始行为?

navigationbar swift xcode11

8
推荐指数
2
解决办法
3893
查看次数

NavigationLink 内的环绕视图会覆盖视图中文本的文本对齐方式以居中

我创建了一个由滚动视图中的视图组成的自定义菜单。

struct MenuInformationDetailView: View {
var title: String = "title"
var subTitle: String = "subTitle"
var imageName: String = "exclamationmark.circle.fill"

var body: some View {
    HStack(alignment: .center) {
        Image(systemName: imageName)
            .font(.largeTitle)
            .foregroundColor(.white)
            .padding()
            .accessibility(hidden: true)

        VStack(alignment: .leading) {
            Text(title)
                .font(.headline)
                .foregroundColor(.white)
                .accessibility(addTraits: .isHeader)

            Text(subTitle)
                .font(.body)
                .foregroundColor(.white)
                .opacity(0.8)
                .fixedSize(horizontal: false, vertical: true)
        }
    }
    .padding(.top)
}
}
Run Code Online (Sandbox Code Playgroud)

然后我将视图包装在 NavigationLink 中以转到所需的目的地:

NavigationLink(destination: PanicDiaryEducationView()) {
        InformationDetailView(title: "Changing behaviour", subTitle: "How to use the behavioural experiments feature", imageName: "questionmark.circle.fill")
                       }
Run Code Online (Sandbox Code Playgroud)

由于某种原因,这样做会导致副标题文本(可能还有标题)居中,而不是默认的前导对齐。使用.frame(alignment : .topLeading) …

xcode swift swiftui swiftui-navigationlink

5
推荐指数
1
解决办法
1253
查看次数

如何从磁盘加载此图像数据并将其显示在我的 SwiftUI 列表中?

我使用imagePickerinSwiftUI从用户的相册中选择图像,并以唯一的 id 将该图像保存到磁盘。这是保存图像(作为数据):

 func saveImage(imageName: String, image: Data) {

guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

let fileName = imageName
let fileURL = documentsDirectory.appendingPathComponent(fileName)

//Checks if file exists, removes it if so.
if FileManager.default.fileExists(atPath: fileURL.path) {
    do {
        try FileManager.default.removeItem(atPath: fileURL.path)
        print("Removed old image")
    } catch let removeError {
        print("couldn't remove file at path", removeError)
    }

}

do {
    try image.write(to: fileURL)
    print("Image ID: \(imageName) saved. Data: \(image)")
} catch let …
Run Code Online (Sandbox Code Playgroud)

xcode image nsdocumentdirectory swift swiftui

4
推荐指数
1
解决办法
4295
查看次数

如何使用 UISearchBar 过滤 NSDictionary?

我需要通过过滤数组来更改我的搜索功能:

func filterContentForSearchText(searchText: String, scope: String = "All") {
    if searchText != "" {

        filteredName = CGCoins.shared.ids.filter {name in
            return   name.lowercased().contains(searchText.lowercased())}
    } else { filteredName = CGCoins.shared.ids

    }
}
Run Code Online (Sandbox Code Playgroud)

过滤包含符号及其相应名称的字典。理想情况下,我希望能够同时搜索键和值,例如,字典 (CGCoins.shared.coinDictionary) 如下所示:

["btc":"bitcoin","eth":"ethereum","ltc":"litecoin"...]
Run Code Online (Sandbox Code Playgroud)

所以我希望能够使用 UISearchBar 进行搜索,并且能够搜索“btc”并返回“bitcoin”或搜索“bitcoin”并返回“bitcoin”。

我试过这个:

func filterNewForSearchText(searchText: String, scope: String = "All") {
    if searchText != "" {

        filteredName = CGCoins.shared.coinDictionary.filter {name in
            return   name.key == searchText}
    } else { filteredName = CGCoins.shared.coinDictionary.values

    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到错误:

无法将“[String : String]”类型的值分配给“[String]”类型

我怎样才能成功地为键和值过滤字典,并返回搜索到的任何内容的值?也欢迎任何替代解决方案。

xcode nsdictionary uisearchbar ios swift

0
推荐指数
1
解决办法
391
查看次数