小编Ant*_*ony的帖子

如何在TableView中调整cell.imageView的大小并在Swift中应用tintColor

我有一个tableView与cellForRowAtIndexPath中创建的单元格并添加一些虚拟文本:

cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "cell")
cell.textLabel?.text = "Test Title"
cell.detailTextLabel?.text = "Test detail label"
Run Code Online (Sandbox Code Playgroud)

然后我将测试图像添加到单元格的imageView:

var image = UIImage(named: "cd.png")
cell.imageView!.image = image
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

要调整颜色,我使用以下代码:

cell.imageView?.tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

结果:

颜色调整细胞imageView

单元格中的图像太大,因此我使用以下代码调整大小:

var itemSize:CGSize = CGSizeMake(20, 20)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
var imageRect : CGRect = CGRectMake(0, 0, itemSize.width, itemSize.height)
cell.imageView!.image?.drawInRect(imageRect)
cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
Run Code Online (Sandbox Code Playgroud)

虽然图像调整大小,但tintColor会丢失:

在此输入图像描述

有什么想法吗?

uitableview ios swift

15
推荐指数
4
解决办法
2万
查看次数

使用简单的 UIToolar 进行键盘 inputAccessoryView 布局约束错误

我遇到了很多布局约束错误,所以我创建了一个新项目,并且只有一个 UIToolbar 和一个 UITextField 来尝试解决问题。即使有一个干净的项目,即使我没有覆盖.inputAccessoryView.

代码如下(无storyboard内容):

import UIKit

class ViewController: UIViewController {

    var aTextField = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()

        let aKeyboardToolBar = UIToolbar()

        // add buttons to the toolbar
        let prevButton = UIBarButtonItem(title: "PREV", style: .done, target: self, action: #selector(prevButtonTapped))
        let nextButton = UIBarButtonItem(title: "NEXT", style: .done, target: self, action: #selector(nextButtonTapped))
        let doneButton = UIBarButtonItem(title: "DONE", style: .done, target: self, action: #selector(doneButtonTapped))
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)

        aKeyboardToolBar.setItems([prevButton, nextButton, flexibleSpace, doneButton], animated: …
Run Code Online (Sandbox Code Playgroud)

uitextfield uitoolbar autolayout inputaccessoryview swift

10
推荐指数
1
解决办法
2700
查看次数

TextEditor (SwiftUI) 中的行限制不起作用

我看到很多答案只是简单地说使用 limit limit view 修饰符,但它似乎不起作用:

mport SwiftUI

struct TextEditorTest: View {
    
    @State var text: String = "Type in here"
    
    var body: some View {

        VStack {
                            
                Text("Sample Text A")
                
                TextEditor(text: $text)
                    .lineLimit(3)
                
                Spacer()
        }
    }
}

struct TextEditorTest_Previews: PreviewProvider {
    static var previews: some View {
        TextEditorTest()
    }
}

Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

swiftui

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

使用 textField.textContentType 时 iOS 键盘闪烁

我发现了一个无法修复的奇怪小错误。在文本字段之间移动时,工具栏(与文本字段的输入附件视图关联)和键盘会闪烁。它仅发生在物理设备上,特别是当文本字段textContentType = .password以及使用代码在文本字段之间移动时,例如在本例中,在文本字段之间翻转的“跳转”工具栏按钮。我附上了几张屏幕截图,显示了故障的情况。它基本上会折叠自动完成密码工具栏一微秒,然后再次显示:

当 .textContentType 未设置时 当 .textContentType 设置时

我测试了其他代码将光标移动到密码文本字段,它也显示了相同的故障。我认为这与通话有关.becomeFirstResponder,但我不知道如何解决它。这种情况不仅发生在.password的选项上.textContentType,也发生在其他选项上,例如.username, .familyName。我需要修复它,因为它不仅会导致轻微的屏幕故障,而且在我的整个项目中,屏幕上有一堆其他东西根据键盘的位置移动,并且这个故障会触发键盘观察器依次更新键盘框架。

我创建了一个空白项目,也发生了同样的问题。这是代码。如果有人可以提供帮助,我们将不胜感激。

class ViewController: UIViewController {

    var keyboardToolbar: UIToolbar!
    var emailTextField : UITextField!
    var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        keyboardToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        let jumpButton = UIBarButtonItem(title: "Jump", style: .plain, target: self, action: #selector(jumpToolBarButtonTapped))
        keyboardToolbar.items = [jumpButton]
        keyboardToolbar.sizeToFit()

        emailTextField = UITextField()
        emailTextField.placeholder = "Email Address TextField"
        emailTextField.keyboardType = .emailAddress
        emailTextField.textContentType = .emailAddress
        emailTextField.autocapitalizationType = .none
        emailTextField.inputAccessoryView …
Run Code Online (Sandbox Code Playgroud)

uikeyboard ios swift

5
推荐指数
0
解决办法
1311
查看次数

Swift 中 UIMenu 中子菜单选项的复选标记

我有以下代码,当用户点击uibarbutton导航栏中的 a 时,它会生成上下文菜单:

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGreen
        title = "Hello"
        
        let scribbleAction =  UIAction(title: "Scribble", image: UIImage(systemName: "scribble"), state: .on, handler: { (_) in })
        let textAction =  UIAction(title: "Typing", image: UIImage(systemName: "textformat"), state: .off, handler: { (_) in })
        
        let clockAction = UIAction(title: "Show Clock", image: UIImage(systemName: "clock"), state: .off, handler: { (_) in })
        let calendarAction = UIAction(title: "Date Last Used", image: UIImage(systemName: "calendar"), state: .off, handler: { …
Run Code Online (Sandbox Code Playgroud)

contextmenu ios swift

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