【问题标题】:Xcode Swift: How to make an UIButton create a new object?Xcode Swift:如何让 UIButton 创建一个新对象?
【发布时间】:2018-03-06 09:05:13
【问题描述】:

我希望右上角的“添加新笔记本”按钮在我单击时创建一个新笔记本。而且我还希望将新笔记本放置在正确的位置。我还没有编写任何代码,因为我什至不知道如何开始编写代码。我知道我必须为按钮创建一个 IBAction,但我不知道下一步是什么。

如果你们能帮忙,我将不胜感激,谢谢!

【问题讨论】:

标签: ios swift xcode uibutton storyboard


【解决方案1】:
@IBOutlet weak var firstNotebook: UIView! // Make a ref to the first notebook

@IBAction func addNotebook(_ sender: UIButton) { 
    let newNotebook = UIView(frame: firstNotebook.frame)  // This is making the new notebook using the first ones frame only thing is you would be putting it right over the first one so you need to make a new X position 
    let newX = firstNotebook.frame.origin.x + 40.0 // I have no idea of how far you want it 
    newNotebook.frame.origin.x = newX
    // So you have a new UIView now, you still need to put it in the superView, the superView is already named view for you

    view.addSubview(newNotebook)
}

这可以让您继续前进,但绝不是正确的方法。正确的方法是添加约束以使其具有适应性,但这对您来说是一个开始祝您好运!

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2015-07-06
    相关资源
    最近更新 更多