【问题标题】:3X4 keypad with round buttons as seen in iPhone Phone App responsive for all screen sizes3X4 键盘,带有圆形按钮,如 iPhone 手机应用程序所示,可响应所有屏幕尺寸
【发布时间】:2019-07-26 01:52:57
【问题描述】:

谁能帮我打破 Apple 创建phone app keypad screen 的方法?我尝试使用 UIStackView 和 1:1 按钮和许多其他方法创建它,但都是徒劳的。我想在自己的项目中使用这样的屏幕进行学习

This 是我想要的,this 是我目前拥有的,Xcode view of storyboard

【问题讨论】:

  • 你需要展示你已经尝试过的东西,并展示结果如何不是你所期望的。

标签: ios swift iphone autolayout uikit


【解决方案1】:

部分解决方案可能涉及自定义(圆形、绿色)按钮。为了创建一个红色背景的按钮(作为对关键操作的警告),我将 NSButton 子类化:

class ColorButton: NSButton

还有它的 draw(...) 方法。

这让我可以控制外观。从 NSButton 继承的所有其他行为。

此外,@IBDesignable 和@IBInspectable 标记公开了InterfaceBuilder 中的自定义控件。

这是最简单的部分。让外观恰到好处(对我来说)是一项不平凡的工作,因为 Cocoa 按钮有一些微妙的行为(例如禁用、突出显示)。响应桌面颜色选项(例如深色模式)也是一个考虑因素。

这是我的 ColorButton 的代码,它再次改变背景颜色而不是形状。

@IBDesignable
class ColorButton: NSButton {

    @IBInspectable var backgroundColor: NSColor = NSColor.controlBackgroundColor
    { didSet { needsDisplay = true } }

    @IBInspectable var textColor: NSColor = NSColor.controlTextColor

    override func draw(_ dirtyRect: NSRect)
    {   
        //  fill in background with regular or highlight color
        var backgroundColor = (isHighlighted ? self.backgroundColor.highlight(withLevel: 0.75) : isEnabled ? self.backgroundColor : NSColor.gray)!
        if  isEnabled == false      { backgroundColor = NSColor.lightGray }
        let textColor = (isEnabled ? self.textColor : NSColor.disabledControlTextColor )
        var rect: NSRect = NSRect(x: 7.0, y: 5.0, width: self.frame.width - 14.0, height: self.frame.height - 13.0) // self.frame//   dirtyRect
        var path = NSBezierPath(roundedRect: rect, xRadius: 4.0, yRadius: 4.0)
        backgroundColor.setFill()
        NSColor.darkGray.setStroke()
        path.lineWidth = 0
        path.fill()
        path.stroke()

        let font = NSFont(name: "Helvetica", size: (rect.height) * 0.66)
        let text = title as NSString
        let textFontAttributes = [convertFromNSAttributedStringKey(NSAttributedString.Key.font): font!, convertFromNSAttributedStringKey(NSAttributedString.Key.foregroundColor): textColor] as [String : Any]
        let size = text.size(withAttributes: convertToOptionalNSAttributedStringKeyDictionary(textFontAttributes))
        let location = NSPoint(x: ((self.frame.width - size.width) / 2.0), y: ((self.frame.height - size.height) / 2.0) - 3.0)
        text.draw(at: location, withAttributes: convertToOptionalNSAttributedStringKeyDictionary(textFontAttributes))
    }// draw
} 

以上是在 Swift 3 下开发的。添加了 Swift 4 迁移工具:

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String {
    return input.rawValue
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
    guard let input = input else { return nil }
    return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}

【讨论】:

  • 我会试试的
  • NSColor、NSButton、NSFont、NSBezierPath、NSPoint 与 UIKit 框架?这是闻所未闻的事情。
【解决方案2】:

https://www.youtube.com/watch?v=vI7m5RTYNng

解释得很好,也有代码。

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多