快速更新课程

时间:2018-10-24 10:21:46

标签: ios swift uibutton

我正在寻找解决问题的方法。我有一个为我创建UIButton的类:

class MenuButton: UIButton {

    var x: CGFloat
    var y: CGFloat
    var width: CGFloat
    var height: CGFloat

    required init(parent: UIView = UIView(), x: CGFloat = 0, y: CGFloat = 0, width: CGFloat = 40, height: CGFloat = 40) {

        self.x = x
        self.y = y
        self.width = width
        self.height = height

        super.init(frame: .zero)

        frame = CGRect(x: x, y: y, width: width, height: height)
        backgroundColor = global.labelColor
        setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        layer.cornerRadius = self.frame.height / 2

        if x < 0 {
            frame = CGRect(x: superview?.frame.width ?? 0 - abs(x) - frame.width, y: frame.minY, width: frame.width, height: frame.height)
        }

        if y < 0 {
            frame = CGRect(x: frame.minX, y: superview?.frame.height ?? 0 - abs(y) - frame.height, width: frame.width, height: frame.height)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

然后我添加一个分配给此类的常量:

let pauseButton = MenuButton()

这很好用,并且按钮在场景中显示为默认设置。现在,我想在viewDidLoad()中添加这些东西(我在类中添加了子视图以访问它):

view.addSubview(pauseButton)
pauseButton.x = -20
pauseButton.y = 20
pauseButton.width = 40
pauseButton.height = 40

这不会更新我在init()中指定的内容。我该如何再次调用该init(而无需再次执行pauseButton = MenuButton())还是可以使用一个update()一样?

谢谢您的帮助;)

4 个答案:

答案 0 :(得分:0)

每个df = pd.DataFrame({'medical_plan_id': [np.nan, '', 2134, 4325, 6543, '', np.nan], 'values': [1, 2, 3, 4, 5, 6, 7]}) df['medical_plan_id'] = df['medical_plan_id'].replace('', np.nan) dfs = dict(tuple(df.groupby(df['medical_plan_id'].isnull()))) print(dfs[0], dfs[1], sep='\n'*2) medical_plan_id values 2 2134.0 3 3 4325.0 4 4 6543.0 5 medical_plan_id values 0 NaN 1 1 NaN 2 5 NaN 6 6 NaN 7 都以UIViewController的身份访问UIView 您已在此处添加子视图。

view

,因此只需将其传递到您拥有的view.addSubview(pauseButton) 中并添加到其中即可。

您的代码可能是这样的。

init()

像这样在class MenuButton: UIButton { var x: CGFloat var y: CGFloat var width: CGFloat var height: CGFloat required init(parent: UIView = UIView(), x: CGFloat = 0, y: CGFloat = 0, width: CGFloat = 40, height: CGFloat = 40) { self.x = x self.y = y self.width = width self.height = height super.init(frame: .zero) frame = CGRect(x: x, y: y, width: width, height: height) backgroundColor = global.labelColor setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal) layer.cornerRadius = self.frame.height / 2 if x < 0 { frame = CGRect(x: superview?.frame.width ?? 0 - abs(x) - frame.width, y: frame.minY, width: frame.width, height: frame.height) } if y < 0 { frame = CGRect(x: frame.minX, y: superview?.frame.height ?? 0 - abs(y) - frame.height, width: frame.width, height: frame.height) } parent.addSubview(self) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } 中使用它。

ViewDidLoad

答案 1 :(得分:0)

实例化按钮时,

init()方法仅被调用一次。您应该尝试使用所需的属性实例化按钮。尝试这样的事情:

let pauseButton = MenuButton(parent: UIView(), x: -20, y: 20, width: 40, height:40)

然后在您的viewDidLoad()方法中,您可以看到以下这一行:

view.addSubview(pauseButton)

我注意到的另一个问题是您在init()方法中引用了Superview,但是在这种情况下Superview始终为nil,因为您的按钮尚未添加为子视图。

答案 2 :(得分:0)

您实际上可以按以下方式在初始化程序中传递这些值,

let pauseButton = MenuButton(parent: view, x: -20, y: 20, width: 40, height: 40)

如果您想在更改任何这些属性时立即更新按钮,则可以使用didSet回调将frame设置如下,

class MenuButton: UIButton {

    var x: CGFloat {
        didSet {
            self.updateFrame()
        }
    }

    var y: CGFloat  {
        didSet {
            self.updateFrame()
        }
    }

    var width: CGFloat  {
        didSet {
            self.updateFrame()
        }
    }

    var height: CGFloat  {
        didSet {
            self.updateFrame()
        }
    }

    private func updateFrame() {
        frame = CGRect(x: x, y: y, width: width, height: height)
    }

    required init(parent: UIView = UIView(), x: CGFloat = 0, y: CGFloat = 0, width: CGFloat = 40, height: CGFloat = 40) {

        self.x = x
        self.y = y
        self.width = width
        self.height = height

        super.init(frame: .zero)

        frame = CGRect(x: x, y: y, width: width, height: height)
        backgroundColor = .green
        setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        layer.cornerRadius = self.frame.height / 2

        if x < 0 {
            frame = CGRect(x: superview?.frame.width ?? 0 - abs(x) - frame.width, y: frame.minY, width: frame.width, height: frame.height)
        }

        if y < 0 {
            frame = CGRect(x: frame.minX, y: superview?.frame.height ?? 0 - abs(y) - frame.height, width: frame.width, height: frame.height)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

答案 3 :(得分:0)

您尝试过吗?

 button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)

或者您可以在菜单按钮类中声明一个名为updateFrame的函数,并将新位置作为参数传递,如下所示:

func updateFrame(x:CGFloat, y: CGFloat, width: CGFloat, height: CGFloat){
self.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
}

并在您的VideDidLoad中:

button.updateFrame(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)

此刻您正在做的只是为Button的属性分配一些值,您的按钮如何知道如何使用它们来重置其框架?