我考虑有什么区别
@Published var isLoggedIn: Bool = false
var isLoggedIn: AnyPublisher<Bool, Never>
Run Code Online (Sandbox Code Playgroud)
我知道在第一种情况下,我可以直接在 SwiftUI View 中使用它,即使用 $ 符号创建绑定,然后例如使用if $isLoggedIn.animation()
但我怎样才能实现同样的目标,AnyPublisher<Bool, Never>
似乎我需要在某个地方调用allocate或sink和store()。为了让它发挥作用。因此,在 SwiftUI 中从它创建绑定似乎是不可能的if $isLoggedIn.animation
@Published 的限制是我无法进行自定义观察。例如 UserDefaults,我可以通过 UserDefaults.publisher 中的 AnyPublisher 来完成。
现在看来我需要两者都拥有并进行AnyPublisher
更新@Published var
但我认为 @Published 是在常规 AnyPublisher 之下的属性包装器,那么为什么我不能直接从 AnyPublisher 在 SwiftUI 视图中进行绑定?
我在为collationStringSelector
. 它没有找到我正确定义的选择器。
这是我的方法调用:
for element in elements {
if let indexable = element as? CollationIndexable {
let collationIndex = collation.section(for: indexable, collationStringSelector: "collationString")
if contentCollationIndexed[collationIndex] == nil {
contentCollationIndexed[collationIndex] = [Element]()
}
contentCollationIndexed[collationIndex]!.append(element)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的元素类型应该实现的协议
@objc protocol CollationIndexable : class {
@objc var collationString : String { get }
}
Run Code Online (Sandbox Code Playgroud)
这是实现协议和选择器属性的具体元素类型
extension Contact : CollationIndexable {
@objc var collationString : String {
return lastName
}
}
Run Code Online (Sandbox Code Playgroud)
更新!
好了,问题解决了,Contact类必须继承form
NSObject
在 iOS 11 和 Swift 4 中使用 ActionSheet 样式呈现 UIPopoverController 或 UIAlertController 时,如何删除/更改 UISegementedControl 上的灰色按钮的默认效果。
uisegmentedcontrol uiactionsheet uipopovercontroller ios swift
我需要Text()
使用+
运算符连接SwiftUI 中的视图
我试过这样的事情
Text("\(feed.author?.firstName ?? "") \(feed.author?.lastName ?? "") ")
.font(.custom("AvenirNext-Medium", size: 15))
.foregroundColor(.black)
ForEach(feed.titleChunks, id: \.self) { chunk in
+ Text("\(chunk)")
.font(.custom("AvenirNext-Regular", size: 15))
.foregroundColor(Color("BodyText"))
}
Run Code Online (Sandbox Code Playgroud)
但它当然不起作用。有没有办法获取使用 Text 打印的未知数量元素的字符串数组,以便它在 SwiftUI 中形成单个文本视图,就像
Text("1") + Text("2") + Text("3")
做?
有没有办法解决这个问题。我厌倦了静态方法并且它有效,但我事先不知道我有多少 Text()
Text("\(feed.author?.firstName ?? "") \(feed.author?.lastName ?? "") ")
.font(.custom("AvenirNext-Medium", size: 15))
.foregroundColor(.black)
+ Text("\(feed.titleChunks[0])")
.font(.custom("AvenirNext-Regular", size: 15))
.foregroundColor(Color("BodyText"))
+ Text("\(feed.titleChunks[1])")
.font(.custom("AvenirNext-DemiBold", size: 15))
.foregroundColor(Color("BodyText"))
Run Code Online (Sandbox Code Playgroud) 我试图通过 UIViewRepresentable 在 SwiftUI 中实现 CollectionView 并在那里使用 SwiftUI 视图作为可重用的单元格,但单元格根本没有出现。在 SwiftUI 中似乎无法包装集合视图并使用组合布局,或者我可能遗漏了一些东西。
我知道我可以在 UIKit 中完全实现这个 CollectionView 并且只包装所有的视图控制器但是如果我想使用 SwiftUI 视图作为 UICollectionView 的单元格怎么办?
当我使用 UICollectionViewFlowLayout() 视图正确显示
import SwiftUI
import UIKit
struct CollectionView<Section: Hashable & CaseIterable, Item: Hashable>: UIViewRepresentable {
// MARK: - Properties
let layout: UICollectionViewLayout
let sections: [Section]
let items: [Section: [Item]]
// MARK: - Actions
let snapshot: (() -> NSDiffableDataSourceSnapshot<Section, Item>)?
let content: (_ indexPath: IndexPath, _ item: Item) -> AnyView
// MARK: - Init
init(layout: UICollectionViewLayout,
sections: [Section],
items: [Section: …
Run Code Online (Sandbox Code Playgroud) 我知道如何使用 Xcode 创建新的 Swift 包我现在如何使用 Swift 包管理器选项卡在项目设置中添加新的 Swift 包
但是我如何刷新它,即强制从 github 下载更新版本。目前我没有用于版本控制的标签,所以我选择使用 master 分支
我目前所做的是删除依赖项并在 Swift Package Manager 中再次添加它。
我的视图层次结构如下: NestedScrollView > ConstraintLayout > [Layout,Layout, RecyclerView]
我希望我的 RecyclerView 填充 Nested ScrollView 中的剩余空间。我的ContaintLayout有wrap_content布局_高度。子布局具有以 dp 单位设置的固定高度。我想设置 RecyclerView 高度来调整 ConstraintLayout 内的剩余空间。
我以编程方式将 ConstraintLayout 的高度设置为计算值,例如两个子布局的高度 + 屏幕高度。我几乎可以工作,但是具有当前wrap_content高度的RecyclerView似乎超出了其父ConstraintLayout边界,不适合其底部边距。如果我约束到父 ConstrintLayout 的底部,那么它将移动到上面的子布局内容上。如果我设置 RecyclerView 的 0dp 高度,那么它的高度为 0dp,不会拉伸到可用空间内。也许唯一的选择是以编程方式将 RecyclerView 的高度设置为固定 dp 大小。onMeasure()、onLayout 或视图、片段等中的其他回调方法?
任何想法?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".ui.billing.BillingFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.domain.AppName.base.ui.billing.BillingNestedScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:overScrollMode="never"
android:fillViewport="true"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@color/theMinteFormBackground">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<com.domain.AppName.base.utils.design.ShadowLayout
android:id="@+id/creditCardSectionLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
myapp:sl_shadow_color="#AAD4D4D4"
myapp:sl_shadow_angle="360"
myapp:sl_shadow_distance="0dp"
myapp:sl_shadow_radius="4dp" …
Run Code Online (Sandbox Code Playgroud) android android-recyclerview android-nestedscrollview android-constraintlayout
我有这样的约束,在 textviews 之间创建了 chain_style 打包,如果 textview 定义了 wrap_content 它不会换行,如果设置为 0dp 它会占用所有可用空间,因此存档标签始终位于布局的右侧,而不是在第一个 textview 之后
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{deal.name}"
tools:text="Deal Name Deal Name Deal Name Deal Name Deal Name Deal Name Deal Name"
style="@style/ItemDealTitleTextAppearance"
android:autoSizeTextType="none"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="24dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
app:layout_constraintHorizontal_chainStyle="packed"
android:layout_marginEnd="100dp"
app:layout_constraintTop_toBottomOf="@id/amountTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@id/verticalFlow"
app:layout_constraintEnd_toStartOf="@id/archivedTextView"
/>
<TextView
android:id="@+id/archivedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/ArchiveLabelStyle"
android:paddingStart="4dp"
android:paddingEnd="4dp"
app:visibleGone="@{deal.isArchived == true}"
android:text="Archived"
android:layout_marginEnd="24dp"
android:layout_marginStart="16dp"
app:layout_constraintStart_toEndOf="@id/nameTextView"
app:layout_constraintTop_toTopOf="@id/nameTextView" />
Run Code Online (Sandbox Code Playgroud)