swift - UISegmentedControl 通过代码自定义

标签 swift class subclass uisegmentedcontrol

我的 UISegmentedControl 有问题。我创建了段类:

import UIKit

class CustomSegmentedControl: UISegmentedControl {

    func AwakeFromNib() {
    super.awakeFromNib()

    let myColor : UIColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    self.layer.masksToBounds = true
    self.layer.borderColor = myColor.cgColor
    self.layer.borderWidth = 0.5
    self.layer.cornerRadius = 8

    }
}

当我尝试改变某些东西时,它没有改变。在我的主 ViewController 中,我标记了 UISegmentedControl 的类文件。

这是 ViewController 中的函数定义:

 @IBAction func ShowInterval(_ sender: UISegmentedControl) {
 }

你能帮我一下吗?非常感谢您!

最佳答案

来自苹果文档:

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized

试试这个:

func layoutSubviews() {
    super.layoutSubviews()
    let myColor : UIColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0)
    self.layer.masksToBounds = true
    self.layer.borderColor = myColor.cgColor
    self.layer.borderWidth = 0.5
    self.layer.cornerRadius = 8
}

关于swift - UISegmentedControl 通过代码自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47227514/

相关文章:

ios - 隐藏 AVPlayerController 全屏按钮

swift - 从 GameScene 类中的节点创建子类

c++ - 有没有办法循环变量? (C++)

php - 用于包装数据库行子类的 OOP 方法

ios - 工具栏上的“从图像库中选择”按钮被停用

ios - Swift 类 Data 和 UITableViewController 运行时错误

c# - 如何在 C# 中将 className 传递给将泛型类型作为参数的方法

java - ProGuard 找不到父类(super class)或接口(interface)

python:如何覆盖str.join?

swift - 如何在 Swift 3 中创建 SKSpriteNode 的子类