You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复UIButton的radius自定义运行时属性警告与IBDesignables异常

Hey there, let's squash those pesky warnings once and for all! 😊

First off, both warnings boil down to the same root issue: Interface Builder and the runtime can't find a property named radius on your UIButtonRounded class, so they can't set its value via Key-Value Coding (KVC). From your code snippet, it looks like your @IBInspectable property was cut off mid-name (rou...), which is almost certainly the problem.

Fix Steps

1. Fix the @IBInspectable Property Definition

First, make sure your custom button class has a properly named, fully defined radius property that matches the key path you're using in IB. Here's the corrected code:

@IBDesignable class UIButtonRounded: UIButton {
    // Match this property name exactly to the "radius" key path in IB
    @IBInspectable var radius: CGFloat = 0.0 {
        didSet {
            // Update the corner radius immediately when the value changes
            updateCornerRadius()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateCornerRadius()
    }
    
    private func updateCornerRadius() {
        layer.cornerRadius = radius
        // Uncomment this if you want to clip the button's content to the rounded corners
        // clipsToBounds = true
    }
}

Key notes here:

  • The property name radius must be exactly the same (case-sensitive) as the key path you set in Interface Builder
  • Adding a didSet observer ensures the corner radius updates instantly when you tweak the value in IB, no need to wait for layout
  • Double-check that updateCornerRadius correctly assigns the value to layer.cornerRadius

2. Clear IB Cache and Rebuild

Interface Builder loves to hold onto old cached class info, even after you fix your code. Do these steps to reset things:

  • Close Xcode and all open Storyboard/XIB windows
  • Delete your project's Derived Data: Go to Xcode > Preferences > Locations, click the arrow next to the Derived Data path, and delete the folder for your project
  • Reopen Xcode, clean the project (Cmd+Shift+K), then build it (Cmd+B)
  • Head back to your Storyboard/XIB, select the button, and reconfirm the Custom Class is set to UIButtonRounded

3. Verify IB Settings

Double-check your button's settings in Interface Builder to avoid typos:

  • Select the button, open the Identity Inspector (third tab in the right panel)
  • Under Custom Class, make sure UIButtonRounded is selected, and set the Module to your current project (avoid "Inherit From Target" if you run into issues)
  • Scroll down to User Defined Runtime Attributes, confirm the key path is radius, the Type is Number, and your desired value is set

Why This Happened

The error [setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key radius
This is a KVC error. When the system tries to set a value using the key path radius, it can't find a matching property on your button class (either because the name was wrong, the property wasn't defined properly, or IB had stale cache data). That's why both the runtime and IB Designables throw warnings.

内容的提问来源于stack exchange,提问作者Rafiqul Hasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:56:16