我来自iOS开发,我想知道是否有一种方法可以在iOS中以NSCollectionView
编程方式设置UICollectionView
?并添加NSCollectionViewItems
代码.或者是设置NSCollectionView
使用绑定的唯一方法?
谢谢!
键盘出现时如何显示完整的列表?键盘隐藏在列表的下部。
我的列表行中有一个textField。当键盘出现时,无法向下滚动以查看完整列表。键盘在列表的前面,而不在列表的“下面”。这是我的编码:
struct ContentView: View {
@State private var name = ""
var body: some View {
List {
VStack {
Text("Begin")
.frame(width: UIScreen.main.bounds.width)
.padding(.bottom, 400)
.background(Color.red)
TextField($name, placeholder: Text("enter text"), onEditingChanged: { _ in
//
}) {
//
}
Text("End")
.frame(width: UIScreen.main.bounds.width)
.padding(.top, 400)
.background(Color.green)
}
.listRowInsets(EdgeInsets())
}
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我怎么做吗?
非常感谢你。
我有一个问题让我疯了几天.
我有一个GridPane,我想在单击按钮时隐藏第一行.
这是FXML文件
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<Button fx:id="buttonTest" mnemonicParsing="false" onAction="#handleButtonTestAction" text="Button" />
<GridPane fx:id="gridPaneTest" gridLinesVisible="true" layoutX="0.5" layoutY="0.5" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label fx:id="labelTopLeft" text="top left">
<font>
<Font size="15.0" />
</font>
</Label>
<Label fx:id="labelTopRight" text="top right" GridPane.columnIndex="1">
<font>
<Font size="15.0" />
</font>
</Label>
<Label text="center left" GridPane.rowIndex="1">
<font>
<Font size="15.0" />
</font>
</Label> …
Run Code Online (Sandbox Code Playgroud)我有一个带有多个按钮的 ScrollView。一个按钮在下面包含一个图像和一个文本。
由于图像非常大,我使用 .scaledToFill 和 .clipped。即使未显示,似乎图像的“剪辑”部分仍然可以点击。
在视频中,您看到我点击了按钮 1,但触发了按钮 2。
这是我的编码。图像位于视图卡内。
struct ContentView: View {
@State var useWebImage = false
@State var isSheetShowing = false
@State var selectedIndex = 0
private let images = [
"https://images.unsplash.com/photo-1478368499690-1316c519df07?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2706&q=80",
"https://images.unsplash.com/photo-1507154258-c81e5cca5931?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2600&q=80",
"https://images.unsplash.com/photo-1513310719763-d43889d6fc95?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2734&q=80",
"https://images.unsplash.com/photo-1585766765962-28aa4c7d719c?ixlib=rb-1.2.1&auto=format&fit=crop&w=2734&q=80",
"https://images.unsplash.com/photo-1485970671356-ff9156bd4a98?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2734&q=80",
"https://images.unsplash.com/photo-1585607666104-4d5b201d6d8c?ixlib=rb-1.2.1&auto=format&fit=crop&w=2700&q=80",
"https://images.unsplash.com/photo-1577702066866-6c8897d06443?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2177&q=80",
"https://images.unsplash.com/photo-1513809491260-0e192158ae44?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2736&q=80",
"https://images.unsplash.com/photo-1582092723055-ad941d1db0d4?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=2700&q=80",
"https://images.unsplash.com/photo-1478264635837-66efba4b74ba?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjF9&auto=format&fit=crop&w=2682&q=80"
]
var body: some View {
NavigationView {
ScrollView {
VStack(spacing: 40) {
Text(useWebImage ? "WebImage is used." : "SwiftUI Image is used")
.font(.system(size: 18))
.bold()
.kerning(0.5)
.padding(.top, 20)
Toggle(isOn: $useWebImage) {
Text("Use WebImage") …
Run Code Online (Sandbox Code Playgroud) 我有一个带有tableView的ViewController.我已经在故事板中进行了设置.有没有办法以编程方式设置tableView的约束?我试图从ViewController中的tableView设置一个IBOutlet并为其添加约束.但那没用.
这是我的ViewController
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
addTableViewConstraints()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addTableViewConstraints() {
tableView.setTranslatesAutoresizingMaskIntoConstraints(false)
var topConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
var leadingContraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
var trailingConstraint = NSLayoutConstraint(item: self.tableView, …
Run Code Online (Sandbox Code Playgroud) 我想使用swift在我的mac osx应用程序中触发鼠标左键.
这是在Objective-C中实现它的方法
[myButton sendActionOn:NSLeftMouseDownMask];
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何在Swift中做到这一点.有人可以帮我解决我的问题吗?
谢谢!
我尝试通过按钮以编程方式在 MultiDatePicker 中选择日期(今天)。选择按预期进行,并且将调用 onChange 修饰符,并且今天在选取器中标记为已选择。但是,当我尝试直接在 MultiDatePicker 中取消选择日期时,日期不再被标记,但 onChange 修饰符将不会被调用。如果您现在点击 MultiDatePicker 中的另一个日期,则两个日期、另一个日期和今天的日期都将标记为已选择。
import SwiftUI
struct ContentView: View {
@Environment(\.calendar) private var calendar
@State private var selectedDates: Set<DateComponents> = []
@State private var onChangeCounter = 0
var body: some View {
VStack {
MultiDatePicker("Select dates", selection: $selectedDates)
.frame(height: UIScreen.main.bounds.width)
.onChange(of: selectedDates) { _ in
self.onChangeCounter += 1
}
Button("Select today") {
let todayDatecomponents = calendar.dateComponents(in: calendar.timeZone, from: Date.now)
selectedDates.insert(todayDatecomponents)
}
.foregroundColor(.white)
.frame(minWidth: 150)
.padding()
.background(Color.accentColor)
.cornerRadius(20)
HStack {
Text("onChangeCounter")
Spacer()
Text(String(onChangeCounter))
} …
Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以在Mac OSX应用程序中旋转NSImage?或者只是使用Swift设置从纵向到横向的方向?
我正在玩CATransform3DMakeAffineTransform,但我无法让它工作.
CATransform3DMakeAffineTransform(CGAffineTransformMakeRotation(CGFloat(M_PI) * 90/180))
Run Code Online (Sandbox Code Playgroud)
这是我第一次使用转换.所以请耐心等待我:)也许我正在采取错误的方法......
有人能帮帮我吗?
谢谢!