简体   繁体   English

快速的自定义视图操作

[英]Custom view action in swift

I created a custom View because different UIViewController are having identical appearance. 我创建了一个自定义View因为不同的UIViewController具有相同的外观。

It appeared to be a whole blank when I add the custom View into UIViewController . 当我将自定义View添加到UIViewController时,它似乎是一片空白。 That doesn't matter. 没关系 What matter is how do I create an action for the invisible Button overlay on the ImageView ? 重要的是,如何为ImageView上的不可见Button覆盖创建action I cannot seem to connect Sent Action of the invisible Button to my UIViewController . 我似乎无法将不可见Button Sent Action连接到我的UIViewController

Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: @Mannopson this is how I did 编辑:@Mannopson这就是我的做法

import UIKit

class BookDetailsView: UIView {

    @IBOutlet var wholeView: UIView!
    @IBOutlet var borderView: UIView!
    @IBOutlet weak var imgBook: UIImageView!
    @IBOutlet weak var txtTitle: UITextField!
    @IBOutlet weak var txtAuthor: UITextField!
    @IBOutlet weak var txtDesc: UITextField!
    @IBOutlet weak var btnUploadPhoto: UIButton!

    //init for custom draw
    override init(frame: CGRect) {
        super.init(frame: frame)
        customInit()
    }

    //init for ib
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        customInit()
//        fatalError("init(coder:) has not been implemented")
    }

    private func customInit(){
        //load xib
        Bundle.main.loadNibNamed("BookDetailsView", owner: self, options: nil)

        //add wholeView as subView
        addSubview(wholeView)

        //auto fit
        wholeView.frame = self.bounds
        wholeView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        borderView.layer.borderWidth = 3
        borderView.layer.cornerRadius = 40
    }

}

The possible reasons for being unable to connect an Outlet are: 无法连接插座的可能原因是:

  • The ViewController (in InterfaceBuilder) hasn't been attached to it's ViewController class ie no custom class has been specified in the Identity Inspector 尚未将ViewController(在InterfaceBuilder中)附加到其ViewController类,即,未在Identity Inspector中指定自定义类
  • The wrong action/outlet pair ie trying to connect a button action to a view 错误的操作/插座对,即尝试将按钮操作连接到视图

In essence, assigning an action to UIImageView is a bad practice. 从本质上讲,将操作分配给UIImageView是一种不好的做法。 You don't have to use image view to handle tap (even though technically you can). 您不必使用图像视图来处理水龙头(即使从技术上讲您也可以)。 Instead, use simple UIButton, remove its text, border and its color, and simply set an image to it. 而是使用简单的UIButton,删除其文本,边框和颜色,然后简单地为其设置图像。 You will get just what you want, without unnecessary overlays and such stuff. 您将得到所需的东西,而没有不必要的覆盖物和类似的东西。 It's easier and is appropriate. 这比较容易并且合适。


Edit (extract from comments which reveal another issue): 编辑 (从揭示另一个问题的评论中提取):

To add custom view to the view hierarchy. 向视图层次结构添加自定义视图。 Most typical way: 最典型的方法:

Create a subclass of UIViewController with that class owning all the outlets. 创建一个拥有所有出口的UIViewController子类。 Then create a separate storyboard file (or you can even use current). 然后创建一个单独的情节提要文件(或者甚至可以使用current)。 Then simply instantiate it programmatically and by doing that, access the view property. 然后只需以编程方式实例化它,然后执行该操作即可访问view属性。 Add that view as a subview to the view hierarchy of currently shown UIViewController . 将该view作为子视图添加到当前显示的UIViewController的视图层次结构中。

Other solutions include: 其他解决方案包括:

1) Use container view to manage nested UIViewController . 1)使用容器视图来管理嵌套的UIViewController

2) Use separate XIB file for your custom view and instantiate it programmatically. 2)为您的自定义视图使用单独的XIB文件,并以编程方式实例化它。

Try something like this: 尝试这样的事情:

class BookDetailsView: UIView {

@IBOutlet var wholeView: UIView!
@IBOutlet var borderView: UIView!
@IBOutlet weak var imgBook: UIImageView!
@IBOutlet weak var txtTitle: UITextField!
@IBOutlet weak var txtAuthor: UITextField!
@IBOutlet weak var txtDesc: UITextField!
@IBOutlet weak var btnUploadPhoto: UIButton!

}

Load the custom view in UIViewController 's viewDidLoad method: UIViewControllerviewDidLoad方法中加载自定义视图:

let customView = Bundle.main.loadNibNamed("YourXIBName", owner: self, options: nil).first as! YourUIViewSubclass

// Customize appearence
customView.btnUploadPhoto.backgroundColor = UIColor.red

// Add an action for this button
customView.btnUploadPhoto.addTarget(self, action: #selector(yourFunctionForUIButton), forControlEvents: .touchUpInside)

self.view.addSubview(customView)

A custom function: 自定义函数:

func yourFunctionForUIButton() { 
}

Hope it helps. 希望能帮助到你。 This not not an answer for your main question and it's just recommendation 这不是您主要问题的答案,而只是建议

You should add gesture to your ImageView. 您应该向ImageView添加手势。

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    imageView.isUserInteractionEnabled = true
    imageView.addGestureRecognizer(tapGestureRecognizer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM