GVKun编程网logo

在Swift中以编程方式创建UIButton(swiftui与combine编程)

15

本文将介绍在Swift中以编程方式创建UIButton的详细情况,特别是关于swiftui与combine编程的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉

本文将介绍在Swift中以编程方式创建UIButton的详细情况,特别是关于swiftui与combine编程的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于ios – Swift:从xib以编程方式创建UIViewController、ios – swift:以编程方式创建UILabel固定宽度,根据文本长度垂直调整大小、ios – 以编程方式设置UIButton的中心 – SWIFT、ios – 在swift中以特定时间以编程方式删除地址簿中的联系人的知识。

本文目录一览:

在Swift中以编程方式创建UIButton(swiftui与combine编程)

在Swift中以编程方式创建UIButton(swiftui与combine编程)

我正在尝试以编程方式构建UI。如何使该动作起作用?我正在用Swift开发。

viewDidLoad中的代码:

override func viewDidLoad() {        super.viewDidLoad()        // Do any additional setup after loading the view, typically from a nib.        let myFirstLabel = UILabel()        let myFirstButton = UIButton()        myFirstLabel.text = "I made a label on the screen #toogood4you"        myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)        myFirstLabel.textColor = UIColor.redColor()        myFirstLabel.textAlignment = .Center        myFirstLabel.numberOfLines = 5        myFirstLabel.frame = CGRectMake(15, 54, 300, 500)        myFirstButton.setTitle("✸", forState: .Normal)        myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)        myFirstButton.frame = CGRectMake(15, -50, 300, 500)        myFirstButton.addTarget(self, action: "pressed", forControlEvents: .TouchUpInside)        self.view.addSubview(myFirstLabel)        self.view.addSubview(myFirstButton)    }        func pressed(sender: UIButton!) {            var alertView = UIAlertView();            alertView.addButtonWithTitle("Ok");            alertView.title = "title";            alertView.message = "message";            alertView.show();        }

答案1

小编典典

您只是在选择器名称的末尾缺少冒号。因为按下需要一个参数,所以冒号必须在该位置。而且,您按下的函数不应嵌套在viewDidLoad中。

override func viewDidLoad() {    super.viewDidLoad()    // Do any additional setup after loading the view, typically from a nib.    let myFirstLabel = UILabel()    let myFirstButton = UIButton()    myFirstLabel.text = "I made a label on the screen #toogood4you"    myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)    myFirstLabel.textColor = UIColor.redColor()    myFirstLabel.textAlignment = .Center    myFirstLabel.numberOfLines = 5    myFirstLabel.frame = CGRectMake(15, 54, 300, 500)    myFirstButton.setTitle("✸", forState: .Normal)    myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)    myFirstButton.frame = CGRectMake(15, -50, 300, 500)    myFirstButton.addTarget(self, action: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside)    self.view.addSubview(myFirstLabel)    self.view.addSubview(myFirstButton)}@objc func pressed(sender: UIButton!) {    var alertView = UIAlertView()    alertView.addButtonWithTitle("Ok")    alertView.title = "title"    alertView.message = "message"    alertView.show()}

编辑:更新以反映Swift 2.2中的最佳实践。应该使用#selector()而不是不推荐使用的文字字符串。

ios – Swift:从xib以编程方式创建UIViewController

ios – Swift:从xib以编程方式创建UIViewController

这几天我快要接近了,我有一个问题.

如果我必须以编程方式创建一个新的UIViewController怎么办?

使用空/新视图控制器很容易:

var controller: UIViewController = UIViewController()
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller,animated: true,completion: nil)

现在,我有一个我想在控制器上加载的xib文件:

var controller: UIViewController = UIViewController(nibName: "FeedDetail",bundle: nil)
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller,completion: nil)

这次崩溃是因为:

'NSInternalInconsistencyException',reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FeedDetail" nib but the view outlet was not set.'

我已经阅读了这个post,我无法理解什么是错的!

解决方法

Sry我从 here那里得到了答案,你错过了类似的东西(你没有在xib中查看所以添加一个然后执行此操作):

如果您使用Xib,请按照此操作

以下步骤

1) open your xib file then right click on files owner and drag to your first view

2) then bind that view with outlet of “view”

希望你能得到它……

ios – swift:以编程方式创建UILabel固定宽度,根据文本长度垂直调整大小

ios – swift:以编程方式创建UILabel固定宽度,根据文本长度垂直调整大小

我已经看到了涉及自动布局的垂直大小调整的答案,但我正在创建的UILabels仅在运行时需要. (我可能需要零到多个这些标签.)

示例(忽略颜色)

>短文本(注意与较长文本相同的宽度):

>更长的文本(注意与较短文本示例相同的宽度,并且添加更多行的文本):

如果文本可以适合固定宽度的一行,则标签不需要垂直调整大小.但是如果有更多字符,标签应该保持垂直扩展以适应这些附加字符.文本应该一行一行地包裹.文本应从标签的左上角开始.

更具体:

let marker = GMSMarker(position: myLatLng)
// see https://stackoverflow.com/a/40211383/1168364 for imageWithView
marker.icon = imageWithView(label) // **how do i create this label?**
marker.map = map // map is a GMSMapView

这些标签可以在屏幕上的任何位置.这适用于地图应用程序,其中每个标签将放置在随机位置.标签的位置彼此没有关系.

解决方法

UIView有两种有用的方法: sizeToFit()和 sizeThatFits(_:)

第一个将视图调整为最小尺寸以适合子视图的内容,第二个视图根本不更改帧,但返回计算的大小:(1)适合所有子视图和(2)不超过参数大小

所以你可以使用sizeThatFits:

let label = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

    label.backgroundColor = UIColor.orange
    label.textColor = UIColor.white
//  label.text = "ultimate Frisbee"
    label.text = "ultimate Frisbee\nin 3 minutes,\nall welcome|2"
    label.numberOfLines = 10
    view.addSubview(label)

    updateLabelFrame()
}

func updateLabelFrame() {
    let maxSize = CGSize(width: 150,height: 300)
    let size = label.sizeThatFits(maxSize)
    label.frame = CGRect(origin: CGPoint(x: 100,y: 100),size: size)
}

输出:


附:您也可以通过自动布局约束解决您的问题,但我不是以编程方式使用它们的忠实粉丝.

ios – 以编程方式设置UIButton的中心 – SWIFT

ios – 以编程方式设置UIButton的中心 – SWIFT

我在UIView里面有一个UIButton,我的UIButton有我在故事板中设置的约束.现在,我想将UIButton的中心设置在UIView的中心.我将如何以编程方式执行此操作?

解决方法

试试.center属性就好
myButton.center = self.view.center

如果需要,您还可以指定x和y.

myButton.center.x = self.view.center.x // for horizontal
myButton.center.y = self.view.center.y // for vertical

ios – 在swift中以特定时间以编程方式删除地址簿中的联系人

ios – 在swift中以特定时间以编程方式删除地址簿中的联系人

我试图以编程方式在特定时间从地址簿中删除联系人.甚至可以在 Swift中使用Apple吗?我已经熟悉CNContactStore,因为我已经开始在电话簿中添加联系人了.授予访问联系人等的权限.但我不知道如何以特定时间以编程方式从地址簿(永远)中删除联系人.

任何帮助表示赞赏!

解决方法

参考:

http://www.ios-blog.co.uk/tutorials/swift/contacts-framework-p2/

解释(来自链接):

删除联系人

“iOS联系人框架为我们提供了deleteContact(:)函数来帮助我们删除联系人.希望你到目前为止已经理解了这个教程,因为我只是概述了这个过程,让你试一试.就像在本教程中我们将实例化一个CNSaveRequest类型的对象,发出我刚刚提到的deleteContact(:)函数并将可变联系人传递给它.然后,就像我们创建联系人或更新的联系人时一样,我们将使用executeSaveRequest(_ :).

请注意,删除表示删除!无法再次获取已删除的联系人.这在模拟器上无关紧要,但您确实需要确保安装协议,以便不删除用户联系人.

那么,你有没有设法让删除工作?好的,好的,我会发布完整的代码,你可以看到.“

解决方案(来自链接):

let predicate = CNContact.predicateForContactsMatchingName("John")
let toFetch = [CNContactEmailAddressesKey]

do{
  let contacts = try store.unifiedContactsMatchingPredicate(predicate,keysToFetch: toFetch)
  guard contacts.count > 0 else{
    print("No contacts found")
    return
  }

  guard let contact = contacts.first else{

return
  }

  let req = CNSaveRequest()
  let mutableContact = contact.mutablecopy() as! CNMutableContact
  req.deleteContact(mutableContact)

  do{
    try store.executeSaveRequest(req)
    print("Success,You deleted the user")
  } catch let e{
    print("Error = \(e)")
  }
} catch let err{
   print(err)
}

今天的关于在Swift中以编程方式创建UIButtonswiftui与combine编程的分享已经结束,谢谢您的关注,如果想了解更多关于ios – Swift:从xib以编程方式创建UIViewController、ios – swift:以编程方式创建UILabel固定宽度,根据文本长度垂直调整大小、ios – 以编程方式设置UIButton的中心 – SWIFT、ios – 在swift中以特定时间以编程方式删除地址簿中的联系人的相关知识,请在本站进行查询。

本文标签: