swift自定义分段控制(代码片段)

author author     2023-01-19     636

关键词:


import UIKit

@IBDesignable class CustomSegmentedControl: UISegmentedControl 

    override init(frame: CGRect) 
        super.init(frame: frame)
        setupView()
    

    required init?(coder: NSCoder) 
        super.init(coder: coder)
        setupView()
    

    func update()
        let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.white ]
        setTitleTextAttributes(attributes, for: .normal)
        setTitleTextAttributes(attributes, for: .selected)
        for index in 0 ..< numberOfSegments 
            if let image = imageForSegment(at: index)?.withRenderingMode(.alwaysOriginal) 
                setImage(image, forSegmentAt: index)
            
        
    

     func setupView() 
        let insets = UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7)
        let backgroundNormal = #imageLiteral(resourceName: "SegmentedControl-Background-Normal").resizableImage(withCapInsets: insets, resizingMode: .stretch)
        let backgroundSelected = #imageLiteral(resourceName: "SegmentedControl-Background-Selected").resizableImage(withCapInsets: insets, resizingMode: .stretch)
        let attributes = [ NSAttributedString.Key.foregroundColor: UIColor.white ]

        setBackgroundImage(backgroundNormal, for: .normal, barMetrics: .default)
        setBackgroundImage(backgroundSelected, for: .selected, barMetrics: .default)
        setDividerImage(#imageLiteral(resourceName: "SegmentedControl-Divider-NormalNormal"), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        setDividerImage(#imageLiteral(resourceName: "SegmentedControl-Divider-SelectedNormal"), forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
        setDividerImage(#imageLiteral(resourceName: "SegmentedControl-Divider-NormalSelected"), forLeftSegmentState: .normal, rightSegmentState: .selected, barMetrics: .default)

        setTitleTextAttributes(attributes, for: .normal)
        setTitleTextAttributes(attributes, for: .selected)
        for index in 0 ..< numberOfSegments 
            if let image = imageForSegment(at: index)?.withRenderingMode(.alwaysOriginal) 
                setImage(image, forSegmentAt: index)
            
        
    

swift自定义类型(代码片段)

查看详情

swift自定义uiviewinspectable(代码片段)

查看详情

swift使用自定义字体(代码片段)

查看详情

swift自定义导航栏(代码片段)

查看详情

swift自定义navigationcontroller子类(代码片段)

查看详情

swift自定义nsview类(代码片段)

查看详情

swift使用avfoundation实现自定义相机(代码片段)

上一篇文章《swift使用AVFoundation实现自定义相机(一)》实现了使用swiftUI自定义相机,这一篇总结使用UIkit框架实现自定义相机。创建项目的时候选择这个。代码ViewController.swift////ViewController.swift//Snapchat////Createdbywhqon2021/8/13.//imp... 查看详情

swift可自定义的工具提示(代码片段)

查看详情

swift可自定义的工具提示(代码片段)

查看详情

swift在swift中创建自定义运算符(代码片段)

查看详情

swift自定义非常容易使用pickerfield(代码片段)

查看详情

swift中心分页自定义布局(代码片段)

查看详情

关闭一个自定义弹出窗口uiviewcontroller并立即显示另一个自定义弹出窗口uiviewcontroller-swift(代码片段)

...用程序。当我按下一个按钮(“保存”)时,我有一个自定义弹出窗口(UIViewController)。在这个弹出窗口中我有另一个按钮,如果我按下它我想要关闭当前的弹出视图控制器,然后立即呈现另一个不同的自定义弹出视图控制器... 查看详情

Swift - 分段控制 - 切换多个视图

】Swift-分段控制-切换多个视图【英文标题】:Swift-Segmentedcontrol-Switchmultipleviews【发布时间】:2015-01-1503:26:55【问题描述】:直到现在我仍然不知道如何在一个视图控制器中切换多个视图。我的故事板是这样的。现在我想在我的... 查看详情

swift在userdefaults中保存自定义类(代码片段)

查看详情

swiftuitabbar在swift中具有自定义高度(代码片段)

查看详情

如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?

】如何在多个视图控制器中使用自定义工具栏而不重复代码和复制元素(swift)?【英文标题】:howtousecustomizedtoolbarinmultipleviewcontrollerwithoutrepeatingthecodeandcopyingtheelements(swift)?【发布时间】:2019-06-1110:48:49【问题描述】:尝试使... 查看详情

swift使用avfoundation实现自定义相机(代码片段)

这篇文章如何创建一个自定义的苹果相机。AVFoundation的介绍可以在官方文档中查看官方介绍的流程图如下:代码ContentView:////ContentView.swift//MyCamera////Createdbywhqon2021/8/15.//importSwiftUIimportAVFoundationstructContentView:Viewva 查看详情