【问题标题】:Custom UIButton with PureLayout带有 PureLayout 的自定义 UIButton
【发布时间】:2014-09-15 13:29:40
【问题描述】:

我正在使用SmileyBorgPureLayout 库来实现一些自动布局魔法。

如何添加UIButtonTypeCustom

普通按钮将使用以下 Pure Layout UIButton 类方法初始化:

self.myButton = [UIButton newAutoLayoutView];

自定义UIButtons 初始化为

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

因为我不能做类似的事情

self.myButton = [[UIButton buttonWithType:UIButtonTypeCustom] newAutoLayoutView];

并且没有'buttonType'属性来设置初始化后 - 我如何组合 在 PureLayout 中创建自定义 UIButton 的这些步骤?

【问题讨论】:

  • 顺便说一下,请注意+[UIView newAutoLayoutView] 方法实际上只做一件事(除了常规实例化):将translatesAutoresizingMaskIntoConstraints 设置为NO。所以不要害怕使用普通的初始化程序,然后只需添加行 view.translatesAutoresizingMaskIntoConstraints = NO 之后。另一个专业提示:每当您在 UIView 上调用 auto... API 之一(例如 [view autoCenterInSuperview]),translatesAutoresizingMaskIntoConstraints 属性会自动设置为 NO 用于您调用该方法的视图:)
  • 啊,太棒了——说得通。感谢@smileyborg 的回复!

标签: ios objective-c iphone uibutton autolayout


【解决方案1】:

UIButton 默认为 UIButtonTypeCustom,因此您可以使用:

self.myButton = [UIButton newAutoLayoutView];

为了好玩,我建议你 NSLog/printf self.myButton.buttonType!

【讨论】:

  • 嗯 - 这很尴尬。干杯贾斯汀有效 - 我有另一段代码隐藏了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-24
  • 2014-11-30
  • 2011-08-10
相关资源
最近更新 更多