小编goh*_*tis的帖子

如何在 SwiftUI 中使用 edgeIgnoringSafeArea,但让子视图尊重安全区域?

我正在构建一个带有列表视图的 SwiftUI UI,我想在列表视图的一部分上放置一个面板,上面有一个按钮。

在底部有安全区域(没有物理主页按钮)的设备上,我希望面板转到屏幕底部。但我想确保面板上的按钮不会延伸到安全区域。

在我的示例代码中,HStack是包含按钮的面板。我尝试添加.edgesIgnoringSafeArea(.bottom)它,但这不允许它扩展到底部。这让我有点困惑,因为同一个列表ZStack延伸到底部安全区域。

当我添加.edgesIgnoringSafeArea(.bottom)到 时ZStackHStack允许(蓝色面板)延伸到屏幕底部的安全区域。这就是我想要的,但那么一旦我这样做,我怎么能告诉Button那个年代的孩子HStack忽视安全区?

我知道如何在 UIKit 中执行此操作,但我真的希望能够在 SwiftUI 中构建此 UI。我可以手动添加一些填充或间隔以Button在安全区域下添加额外的填充,但随后在没有底部安全区域的带有主页按钮的设备上会有额外的间距。我想找出一个优雅的解决方案,我可以依靠系统来定义其安全区域,而不是手动定义任何数字间距或为不同设备创建条件情况。

struct ContentView: View {
    var body: some View {

        ZStack(alignment: .bottom) {

                    List {
                        Text("Item 1")
                        Text("Item 2")
                        Text("Item 3")
                    }

                    HStack {
                        Spacer()
                        Button(action: {
                            // do something
                        }){
                            Text("Button")
                                .font(.title)
                                .padding()
                                .background(Color.green)
                                .foregroundColor(.white)
                                .cornerRadius(15)
                        }.padding()
                        Spacer()
                    }.background(Color.blue).edgesIgnoringSafeArea(.bottom)
                }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要的失败尝试的屏幕截图

ios swiftui

40
推荐指数
3
解决办法
1万
查看次数

UserDefaults 绑定与 SwiftUI 中的切换

我试图找出构建绑定到 UserDefaults 的简单设置屏幕的最佳方法。

基本上,我有一个 Toggle 并且我想要:

  • 每当此 Toggle 更改时要保存的 UserDefault 值(UserDefault 应该是事实来源)
  • Toggle 始终显示 UserDefault 的值

带有切换功能的设置屏幕

我已经观看了许多 SwiftUI WWDC 会议,但我仍然不确定应该如何使用 Combine 和 SwiftUI 中提供的不同工具来设置所有内容。我目前的想法是我应该使用 BindableObject 以便我可以使用 hat 来封装许多不同的设置。

我想我很接近,因为它几乎按预期工作,但行为不一致。

当我在设备上构建和运行它时,我打开它并打开 Toggle,然后如果我向上和向下滚动视图一点开关切换回关闭(好像它实际上没有保存 UserDefaults 中的值)。

但是,如果我打开开关,离开应用程序,然后再回来,它仍然打开,就像它记住了设置一样。

有什么建议?我发布此内容是希望它能帮助其他不熟悉 SwiftUI 和 Combine 的人,因为我找不到关于此主题的任何类似问题。

import SwiftUI
import Combine

struct ContentView : View {

    @ObjectBinding var settingsStore = SettingsStore()

    var body: some View {
        NavigationView {
            Form {
                Toggle(isOn: $settingsStore.settingActivated) {
                    Text("Setting Activated")
                }
            }
        }.navigationBarTitle(Text("Settings"))
    }
}

class SettingsStore: BindableObject {

    var didChange …
Run Code Online (Sandbox Code Playgroud)

ios swiftui combine

19
推荐指数
3
解决办法
8533
查看次数

INSTALLED_APPS中的干草堆导致错误:无法导入名称openProc

我现在很困惑.我有一个Django项目一直很好,直到我试图添加Haystack/Whoosh进行搜索.我在其他项目中有相同的堆栈工作正常.

每当我在我的settings.INSTALLED_APPS中有"haystack"并且我尝试manage.py runserver或者manage.py shell我得到'错误:无法导入名称openProc'

我认为这可能是Haystack的依赖,但没有正确安装,所以我从网站包中删除了Haystack并重新安装,但同样的事情不断发生.谷歌搜索openProc和相关的关键字没有任何结果.

我希望其他人遇到这个错误,或者至少现在谷歌会有一些可能有答案的东西!我知道这些cannot import name <something>错误可能很棘手,但是这一点让我特别难过,因为它与外部包有关.

python django-haystack

16
推荐指数
2
解决办法
3793
查看次数

Django管理员登录页面在正确的登录凭据上重定向到同一页面

我正在运行一个相对较新的Django应用程序,我没有添加太多内容.我尝试设置管理员(我已经在很多其他应用程序上完成)在构建应用程序时创建一些示例数据,但每当我尝试使用我创建的超级用户帐户登录时,我都会被踢回登录屏幕.

当我输入不正确的凭据时,我看到正确的错误消息...当我有正确的凭据时,我无法让它通过登录屏幕到管理员.有没有其他人有这个问题,并能够解决它?我正处于死胡同的故障排除.

django django-admin

13
推荐指数
2
解决办法
2948
查看次数

在iOS 10中使用UNUserNotificationCenterDelegate收到新通知时,如何删除以前发送的通知?

这个问题是关于UserNotificationsiOS 10中的新框架.

我有一个应用程序,在用户在应用程序中执行某个操作后,每隔半小时安排一次本地通知,从1小时开始.

为了避免使用户的锁定屏幕或通知中心混乱,我只希望一次显示一个通知,因此只有一个通知包含最新的相关信息.我实现这一目标的计划是在出现新通知时随时清除所有已发送的通知.

看来这应该是可行的新willPresent方法UNUserNotificationCenterDelegate,但它的行为并不像我期望的那样.

这是一个函数,我打电话设置所有通知,从应用程序中的事件后1小时开始,并在事件发生后23.5小时每半小时安排一次通知,直到最后一个通知:

func updateNotifications() {

    for hour in 1...23 {
        scheduleNotification(withOffsetInHours: Double(hour))
        scheduleNotification(withOffsetInHours: Double(hour) + 0.5)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是根据实际进度通知的功能mostRecentEventDate,这是一个Date集内其他:

func scheduleNotification(withOffsetInHours: Double) {

    // set up a Date for when the notification should fire
    let offsetInSeconds = 60 * 60 * withOffsetInHours
    let offsetFireDate = mostRecentEventDate.addingTimeInterval(offsetInSeconds)

    // set up the content of the notification
    let content = UNMutableNotificationContent()
    content.categoryIdentifier = "reminder"
    content.sound = UNNotificationSound.default()
    content.title = …
Run Code Online (Sandbox Code Playgroud)

notifications ios ios10 unusernotificationcenter

11
推荐指数
1
解决办法
4651
查看次数

在Django中创建用户通知系统

我正在添加一个系统,为下次登录时可以显示的用户留下"通知".我在models.py文件中创建了一个简单的Notification类.我有这个UserInfo类(在相同的models.py中)作为socialauth的一部分将一些属性添加到Django的现有用户系统:

class UserInfo(models.Model):
    user = models.OneToOneField(User, unique=True)
    ...
    reputation = models.IntegerField(null=True, blank=True)

    def add_notification(message):
        notification = Notification(user=self.user, message=message)
        notification.save
Run Code Online (Sandbox Code Playgroud)

当我在控制台中尝试时,我最终得到了这个:

>>> user = User.objects.get(id=14)
>>> user.userinfo.add_notification('you are an awesome intern!')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: add_notification() takes exactly 1 argument (2 given)
>>> 
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?我是一个Django noob,所以也许这很简单.谢谢!

django-models django-authentication django-socialauth

9
推荐指数
2
解决办法
1万
查看次数

与SwiftUI相等的子视图宽度

我正在尝试使用SwiftUI构建一个简单的watchOS UI,并在按钮上方并排放置两条信息。

我想每一侧(表示为VStackHStack)占用一半的可用宽度的(因此它是黄色的父视图内的偶数50/50)划分,其中| 在下面的示例中,字符位于按钮的中心。

我想要短期长期!!!文字以每边50%为中心。

我从以下代码开始,以使元素就位并显示一些不同堆栈的边界:

var body: some View {
    VStack {
        HStack {

            VStack {
                Text("Short").font(.body)
            }
                .background(Color.green)

            VStack {
                Text("Longer!!!").font(.body)
            }
                .background(Color.blue)

        }
            .frame(minWidth: 0, maxWidth: .infinity)
            .background(Color.yellow)
        Button (action: doSomething) {
            Text("|")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这给了我这个结果: 初始点

然后,当将每个并排VStack宽度设置为可用宽度的50%时,我陷入了困境。我认为将其添加.relativeWidth(0.5)到每个对象中应该是可行的VStack,据我所知,应该将每个对象VStack的宽度都设为其父视图的宽度(HStack带有黄色背景的):

var body: some View {
    VStack {
        HStack {

            VStack {
                Text("Short").font(.body)
            }
                .relativeWidth(0.5)
                .background(Color.green)

            VStack {
                Text("Longer!!!").font(.body)
            }
                .relativeWidth(0.5) …
Run Code Online (Sandbox Code Playgroud)

watchos swiftui

9
推荐指数
2
解决办法
1387
查看次数

使用jQuery的对象ID创建一个数组,以便在Django后端上进行处理

我有一些javascript正在创建一个基于AJAX搜索后从后端返回的对象的li.用户可以继续搜索设备,并在选择时将它们添加到当前页面(作为li).每次创建新的li时,我都想发送已经选择的对象的id.

创建li时,它们的id被命名为"device - ###",其中###是数据库中设备的ID,因此我需要删除该部分.

这是给我问题的javascript:

        var children = $('#temp_inventory').children();
        var count = children.length;
        var devices = [];
        var i = 0;
        while (i<=count){
            devices[i] = children[i].id.substr(4);
            i++;
        };
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Uncaught TypeError: Object #<HTMLLIElement> has no method 'attr'
Run Code Online (Sandbox Code Playgroud)

我也尝试了非jQuery方式:

devices[i] = children[i].id.substr(4);
Run Code Online (Sandbox Code Playgroud)

我最终得到了这个错误:

Uncaught TypeError: Cannot read property 'id' of undefined
Run Code Online (Sandbox Code Playgroud)

当我投入时,alert(children[i].id.substr(4));我得到了一个我正在期待的号码的警报.

javascript arrays django jquery

6
推荐指数
1
解决办法
2138
查看次数

使用 SwiftUI 切换更改的动画

我正在构建一个简单的设置屏幕。

当第一个设置被激活时,速度控制出现。当它关闭时,速度控制消失。

根据我对 SwiftUI 的了解,它应该根据下面的代码自动设置动画,但它只是出现和消失。

我怎样才能用 SwiftUI 使这个动画更好看,让它从它上面的单元格滑下来,就像在这个演示文稿中 53:00 一样?

设置动画的GIF

import SwiftUI
import Combine

struct ContentView : View {

    @ObjectBinding var settingsStore: SettingsStore

    var body: some View {
        NavigationView {
            Form {
                Toggle(isOn: $settingsStore.settingActivated.animation(.basic(duration: 3, curve: .easeInOut))) {
                    Text("Setting Activated")
                }
                if settingsStore.settingActivated {
                    VStack(alignment: .leading) {
                        Text("Speed Control")
                        HStack {
                            Image(systemName: "tortoise")
                            Slider(value: .constant(10), from: 0, through: 50, by: 1)
                            Image(systemName: "hare")
                        }
                    }.transition(.opacity)
                }
            }.navigationBarTitle(Text("Settings"))
        }
    }
}

class SettingsStore: BindableObject {

    let didChange = PassthroughSubject<Void, …
Run Code Online (Sandbox Code Playgroud)

ios swiftui combine

6
推荐指数
1
解决办法
1427
查看次数

即使函数体内没有任何内容, userNotificationCenter(_:didReceive:) 怎么会导致崩溃?

我有一个带有通知操作的应用程序。多年来,使用符合UNUserNotificationCenterDelegate其方法并处理其操作的类,一切都运行良好userNotificationCenter(_:didReceive:withCompletionHandler)

\n

最近,我重构了很多我的 app\xe2\x80\x99s 代码以使用 async/await。因此,我切换到委托方法的异步版本,userNotificationCenter(_:didReceive:) async它不再使用完成处理程序。

\n

在发布使用该方法的异步版本的更新后,我开始在我的设备上和野外看到大量崩溃。这里\xe2\x80\x99s 是一个示例,删除了应用程序名称:

\n
Exception Type:  EXC_CRASH (SIGABRT)\nException Codes: 0x0000000000000000, 0x0000000000000000\nTriggered by Thread:  16\n\nApplication Specific Information:\nabort() called\n\n\nLast Exception Backtrace:\n0   CoreFoundation                         0x197ba2248 __exceptionPreprocess + 164\n1   libobjc.A.dylib                        0x190f63a68 objc_exception_throw + 60\n2   Foundation                             0x19252281c _userInfoForFileAndLine + 0\n3   UIKitCore                              0x19aa4fe94 -[UIApplication _performBlockAfterCATransactionCommitSynchronizes:] + 404\n4   UIKitCore                              0x19aa5c20c -[UIApplication _updateStateRestorationArchiveForBackgroundEvent:saveState:exitIfCouldNotRestoreState:updateSnapshot:windowScene:] + 528\n5   UIKitCore                              0x19aa5c4d0 -[UIApplication _updateSnapshotAndStateRestorationWithAction:windowScene:] + 144\n6                                          0x1006c36a8 @objc closure #1 in NotificationDelegate.userNotificationCenter(_:didReceive:) + 132\n7                                          0x1006c37d1 partial apply for @objc closure #1 …
Run Code Online (Sandbox Code Playgroud)

ios unusernotificationcenter

6
推荐指数
1
解决办法
1294
查看次数