首页 文章

使UISlider高度更大?

提问于
浏览
30

我一直在寻找一种让 UISlider 进度条更高的方法,即增加高度 . 但是,我没有想要使用自定义图像或任何东西,只是让它更高,所以 UISlider 看起来不那么薄 . 我错过了一些非常明显的东西吗

5 回答

  • 13

    这是我最近的实施,基于CularBytes的......

    open class CustomSlider : UISlider {
        @IBInspectable open var trackWidth:CGFloat = 2 {
            didSet {setNeedsDisplay()}
        }
    
        override open func trackRect(forBounds bounds: CGRect) -> CGRect {
            let defaultBounds = super.trackRect(forBounds: bounds)
            return CGRect(
                x: defaultBounds.origin.x,
                y: defaultBounds.origin.y + defaultBounds.size.height/2 - trackWidth/2,
                width: defaultBounds.size.width,
                height: trackWidth
            )
        }
    }
    

    通过设置自定义类
    Custom class setting
    在故事板中的UISlider上使用它

    IBInspectable允许您从故事板设置高度
    Height from storyboard

  • -1

    接受的答案将使用 minimumValueImagemaximumValueImage 不合需要地更改滑块's width in some cases, like if you' . 如果您只想更改高度并保留其他所有内容,请使用以下代码:

    override func trackRect(forBounds bounds: CGRect) -> CGRect {
       var newBounds = super.trackRect(forBounds: bounds)
       newBounds.size.height = 12
       return newBounds
    }
    
  • 23

    对于那些希望看到一些改变轨道大小的工作代码的人 .

    class CustomUISlider : UISlider {
    
        override func trackRect(forBounds bounds: CGRect) -> CGRect {
    
            //keeps original origin and width, changes height, you get the idea
            let customBounds = CGRect(origin: bounds.origin, size: CGSize(width: bounds.size.width, height: 5.0))
            super.trackRect(forBounds: customBounds)
            return customBounds
        }
    
        //while we are here, why not change the image here as well? (bonus material)
        override func awakeFromNib() {
            self.setThumbImage(UIImage(named: "customThumb"), for: .normal)
            super.awakeFromNib()
        }
    }
    

    唯一剩下的就是更改故事板中的类:

    storyboardstuff

    您可以继续使用搜索栏操作和插座到对象类型UISlider,除非您想在滑块上添加更多自定义内容 .

  • 18

    我找到了我要找的东西 . 以下方法只需要在子类中进行编辑 .

    - (CGRect)trackRectForBounds:(CGRect)bounds{
        CGRect customBounds = ...
        return customBounds;
    }
    
  • 52

    你可以玩这个,看看会发生什么:

    slider.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 2.0);
    

相关问题