我如何以编程方式创建图形元素(如UIButton)在Swift?我试图创建和添加按钮到一个视图,但不能。
当前回答
将此代码添加到viewDidLoad中 / /添加按钮
var button=UIButton(frame: CGRectMake(150, 240, 75, 30))
button.setTitle("Next", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.backgroundColor = UIColor.greenColor()
self.view.addSubview(button)
将这个函数写在它的外面,当你点击按钮时,这个函数将被调用
func buttonTapAction(sender:UIButton!)
{
println("Button is working")
}
其他回答
这对我来说非常好,#DynamicButtonEvent #IOS #Swift #Xcode
func setupButtonMap(){
let mapButton = UIButton(type: .system)
mapButton.setImage(#imageLiteral(resourceName: "CreateTrip").withRenderingMode(.alwaysOriginal), for: .normal)
mapButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
mapButton.contentMode = .scaleAspectFit
mapButton.backgroundColor = UIColor.clear
mapButton.addTarget(self, action: #selector(ViewController.btnOpenMap(_:)), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: mapButton)
}
@IBAction func btnOpenMap(_ sender: Any?) {
print("Successful")
}
从storyboard创建UIButton: 1 -将UIButton对象从对象库拖到storyboard文件中的ViewController 2 -节目助理编辑 3 -从上面创建的UIButton中右键拖动到你的类中。结果如下:
@IBAction func buttonActionFromStoryboard(sender: UIButton)
{
println("Button Action From Storyboard")
}
为创建UIButton编程: 1-写入“override func viewDidLoad()”:
let uiButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
uiButton.frame = CGRectMake(16, 116, 288, 30)
uiButton.setTitle("Second", forState: UIControlState.Normal);
uiButton.addTarget(self, action: "buttonActionFromCode:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(uiButton)
2-添加IBAction功能:
@IBAction func buttonActionFromCode(sender:UIButton)
{
println("Button Action From Code")
}
如何使用Swift 3.0来做到这一点。
func createButton() {
let button = UIButton(type: .system)
button.frame = CGRect(x: 100.0, y: 100.0, width: 100.0, height: 100.0)
button.setTitle(NSLocalizedString("Button", comment: "Button"), for: .normal)
button.backgroundColor = .green
button.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
view.addSubview(button)
}
@objc func buttonAction(sender: UIButton) {
print("Button pushed")
}
Uilabel code
var label: UILabel = UILabel()
label.frame = CGRectMake(50, 50, 200, 21)
label.backgroundColor = UIColor.blackColor()
label.textColor = UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "test label"
self.view.addSubview(label)
这是可能的。除了使用swift语法之外,您几乎可以以相同的方式完成所有操作。例如,你可以用这样的代码创建一个UIButton:
var button: UIButton = UIButton(frame: CGRectMake(0, 0, 100, 100))
推荐文章
- 改变UISegmentedControl的字体大小
- 我可以强制UITableView隐藏分隔符之间的空单元格吗?
- 获取用户当前位置/坐标
- 获得推送通知,而应用程序在前台iOS
- 如何取消选定的UITableView单元格?
- 设置自定义UITableViewCells的高度
- 在SwiftUI中创建一个VStack填充屏幕宽度
- 移动文本字段时,键盘出现迅速
- 让iPhone震动
- 将NSString转换为NSDate(然后再转换回来)
- 如何在swift中逆向循环迭代?
- applicationwillenter前台vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground
- 如何在扑动中格式化日期时间
- Xcode 8 / Swift 3: "Expression of type UIViewController?未使用”警告
- 我如何使成员初始化公共,默认情况下,在Swift的结构?