我试图获取一个属性字符串的rect,但是boundingRectWithSize调用不符合我传入的大小并且返回一个具有单行高度而不是大高度的矩形(它是一个长字符串).我已经通过传递一个非常大的高度值和0以及下面的代码进行了实验,但是返回的rect总是相同的.
CGRect paragraphRect = [attributedText boundingRectWithSize:CGSizeMake(300,0.0)
options:NSStringDrawingUsesDeviceMetrics
context:nil];
Run Code Online (Sandbox Code Playgroud)
这是否已损坏,或者我是否需要做其他事情以使其返回包裹文本的矩形?
我有一个UITextView
接受一个NSString
与格式stringWithUTF8String
.它从数据库获取它的值,我希望数据库中的文本在文本中呈现中断.我尝试使用\n
这样做,但它被渲染为文本.在我的应用程序的信息页面中以直接文本的方式执行此操作,但我认为从数据库中取出它时不起作用的原因是因为格式化.
有什么建议?
我正在尝试为我的Form
或者更确切地说其中的细胞赋予动画效果。我的问题是,下面的代码给了我一个很好的插入动画,但是对于删除,单元格在看起来很丑陋的延迟后突然被删除。
import SwiftUI
struct ContentView: View {
@State var toggledValue = false
@State var pickedValue = 0
var body: some View {
NavigationView {
Form {
Section {
Toggle(isOn: $toggledValue) {
Text("Toggled Value")
}
if toggledValue {
Picker(selection: $pickedValue, label: Text("Picked Value")) {
ForEach((0...5).identified(by: \.self)) {
Text("Pick Value \($0)").tag($0)
}
}
}
}
Section {
Text("Some Text")
}
}
.navigationBarTitle("Navigation Bar Title")
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试的是将其包装Toggle
在一个withAnimation
闭包中,但这并没有改变任何东西。让我想知道的是,使用List
而不是相同的代码Form
给了我预期的动画。这是一个错误还是我在监督某些事情?
我正在尝试实现需要委托方法(如NSUserActivity
)的功能。因此,我需要一个UIViewController
符合NSUserActivityDelegate
(或类似的其他代表),处理并保存所有必需信息的。我的问题是我在界面上使用的是SwiftUI,因此我没有使用UIViewControllers
。因此,我该如何实现此功能,同时仍将SwiftUI用于UI。我尝试了什么:view1只是一个普通的SwiftUI View
,可以(通过NavigationLink
)呈现view2,这是要实现此功能的视图。因此,我尝试了以下方法,而不是链接view1和view2,而是将view1链接到UIViewControllerRepresentable
,然后处理该功能的实现并添加UIHostingController(rootView: view2)
为子视图控制器。
struct view1: View {
var body: some View {
NavigationLink(destination: VCRepresentable()) {
Text("Some Label")
}
}
}
struct view2: View {
var body: some View {
Text("Hello World!")
}
}
struct VCRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return implementationVC()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
}
class implementationVC: UIViewController, SomeDelegate for functionality {
// does implementation …
Run Code Online (Sandbox Code Playgroud) 我尝试以与使用相同的方式执行此操作UITextField
:
textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.endOfDocument)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
!注意:我见过一些其他问题报告有关出现黑条的问题,但该问题不是重复的(如果我错了,请纠正我),对我来说,当我执行推送转场时,会出现类似黑色视图的内容。黑条问题的解决方案都不适合我(区别:我出现了黑色视图)。我在故事板中使用了标准的“show”segue。以下是viewDidLoad
我的第一个 vc中的代码UIViewController
(此 vc 执行到我的第二个 vc 的转接):
if #available(iOS 11.0, *) {
navigationController!.navigationBar.prefersLargeTitles = false
}
mapView.delegate = self
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
这是我的第二个 vc 中的代码viewDidLoad
(segue 通向此 vc):
if #available(iOS 11.0, *) {
navigationController!.navigationBar.prefersLargeTitles = true
}
Run Code Online (Sandbox Code Playgroud)
他们都使用相同的navigationController
. 对于我的第一个 vc,我想要 navBar preferresLargeTitles = false
,对于我的第二个 vc,我想要 navBar,preferresLargeTitles = true
这就是我在viewDidLoad
方法中所做的事情。MapKit
关于和 的5 行CoreLocation
不应导致这种效果,但我不知道为什么会出现这个黑色视图,所以我留下了这些代码行。当执行转场时,如何删除出现在导航栏后面的黑色视图?(当segue完成后黑色消失)(右上角的黑色是困扰我的)
uinavigationbar uinavigationcontroller segue uistoryboardsegue swift
我正在尝试将 my 的代表设置titleTF
为 myUITableViewCell
或 my UITableView
。这就是我在班级中设置文本字段的方式UITableViewCell
。
class AddAppointmentTableViewCell: UITableViewCell, UITextFieldDelegate, UITextViewDelegate {
var view = UIView()
var titleTF = UITextField()
func addTitle() -> UIView {
view = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height:
NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray]).size().height+30))
titleTF.translatesAutoresizingMaskIntoConstraints = false
titleTF.textAlignment = .left
titleTF.delegate = self
titleTF.attributedText = NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray])
titleTF.inputAccessoryView = toolBar
view.addSubview(titleTF)
titleTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 15).isActive = …
Run Code Online (Sandbox Code Playgroud) func load<T: Decodable>(_ filename: String, as type: T.Type = T.self) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
load("jsonFilePath")
Run Code Online (Sandbox Code Playgroud)
该函数从 JSON 中提取数据。这个“参数”type: T.Type = T.self
是干什么用的?如果我删除此参数,代码仍然有效。那我需要它做什么?
我使用的是最新版本的 Xcode (11 Beta 16) 和 macOS (10.15 Beta 6)
我正在尝试创建两个视图。从第一个视图中,您应该能够通过尾随导航栏项目导航到第二个视图,并且向后导航您应该能够使用系统生成的后退按钮(有效)和附加的尾随导航栏按钮(有一些额外的功能,比如保存数据,但这对我的问题并不重要)。
选项 1 确实有效,但如果您注释掉选项 1 并取消注释选项 2(我想要的布局),则完成按钮不会返回。
struct ContentView1: View {
@State var show = false
var body: some View {
NavigationView {
Form {
Text("View 1")
// Option 1 that does work
NavigationLink(destination: ContentView2(show: $show), isActive: $show) {
Text("Move")
}
}
.navigationBarTitle(Text("Title"))
// Option 2 that does NOT work
// .navigationBarItems(trailing: NavigationLink(destination: ContentView2(show: $show), isActive: $show) {
// Text("Move")
// })
}
}
}
struct ContentView2: View {
@Binding …
Run Code Online (Sandbox Code Playgroud) swift ×7
swiftui ×3
delegates ×2
uitextview ×2
animation ×1
forms ×1
generics ×1
nsstring ×1
objective-c ×1
segue ×1
types ×1
uitableview ×1
uitextrange ×1
utf-8 ×1
whitespace ×1