【问题标题】:Set custom UIButton frame using NSLayoutConstraints使用 NSLayoutConstraints 设置自定义 UIButton 框架
【发布时间】:2015-12-26 14:39:21
【问题描述】:

所以我正在使用这个自定义 UIButton 类:https://github.com/victorBaro/VBFPopFlatButton

并且在文档中,作者使用了这个例子。

//Example
self.flatRoundedButton = [[VBFPopFlatButton alloc]initWithFrame:CGRectMake(100, 150, 30, 30)
                                          buttonType:buttonMenuType
                                         buttonStyle:buttonRoundedStyle
                                         animateToInitialState:YES];
self.flatRoundedButton.roundBackgroundColor = [UIColor whiteColor];
self.flatRoundedButton.lineThickness = 2;
self.flatRoundedButton.tintColor = [UIColor flatPeterRiverColor];
[self.flatRoundedButton addTarget:self
                       action:@selector(flatRoundedButtonPressed)
             forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.flatRoundedButton];

由于buttonType 是一个只能获取的属性,我不能只创建一个VBFPopFlatButton 实例然后分配类型,即我必须在创建实例时分配类型。我只是想知道是否有一种方法可以在无需显式设置框架的情况下进行初始化(使用CGRectMake),因为我想稍后使用约束来设置按钮的框架。

如果有办法在初始化之后覆盖CGRectMake

【问题讨论】:

  • 我认为您需要更改 VBFPopFlatButton 的实现
  • 据我了解,你想改变原点(x和y),大小(高度和宽度)吗?
  • 是的,所以根据文档和实现,我必须在初始化VBFPopFlatButton 时设置框架。我稍后添加的约束似乎确实会影响按钮的原点和大小,但看起来像 CGRectMake 并且约束相互干扰。

标签: ios objective-c uibutton


【解决方案1】:

VBFPopFlatButton 没有 buttonType 属性。它有一个currentButtonType 属性,可以读/写,因此您可以稍后指定类型。

无论如何,只需将框架设置为CGRectZero。然后关闭translatesAutoresizingMaskIntoConstraints

self.flatRoundedButton = [[VBFPopFlatButton alloc] initWithFrame:CGRectZero
    buttonType:buttonMenuType buttonStyle:buttonRoundedStyle animateToInitialState:YES];
self.flatRoundedButton.translatesAutoresizingMaskIntoConstraints = NO;

然后你可以为按钮添加任何你想要的约束。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2010-10-25
    • 2010-09-09
    • 1970-01-01
    • 2020-09-29
    • 2020-02-25
    • 2011-08-19
    相关资源
    最近更新 更多