我正在使用SJSegmentedViewController对视图控制器进行分段。
我有一个名为PlaceReviewsVC
的视图控制器,在其中添加了textView进行审阅。为了发布此评论,我在Post
的{{1}}的{{1}}上添加了UIBarButtonItem
的{{1}}的bar按钮实例,它们是navigationItem.rightBarButtonItem
的实例。
现在的问题是,当我单击发布按钮时,我无法关闭键盘。
我尝试使用以下代码在分段视图控制器中添加发布按钮。
placeDetailSegmentController
在SJSegmentedViewController
方法中,我编写了以下代码来关闭键盘。但是没用。
let headerVC = PlaceImagesVC.viewController()
let reviewVC = PlaceReviewsVC.viewController()
reviewVC.delegate = self
reviewVC.title = "Reviews"
let placeDetailSegmentController = SJSegmentedViewController()
placeDetailSegmentController.delegate = self
placeDetailSegmentController.title = "Pedal Studio"
placeDetailSegmentController.headerViewController = headerVC
placeDetailSegmentController.segmentControllers = [reviewVC]
placeDetailSegmentController.headerViewHeight = 280
placeDetailSegmentController.selectedSegmentViewHeight = 2.0
placeDetailSegmentController.selectedSegmentViewColor = .white
let button = UIButton(type: .custom)
button.setImage(#imageLiteral(resourceName: "icon_back").withRenderingMode(.alwaysTemplate), for: .normal)
button.tintColor = .white
button.addTarget(self, action:#selector(backButtonTapped), for:.touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
button.contentMode = .scaleAspectFit
button.imageEdgeInsets = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
let leftButton = UIBarButtonItem(customView: button)
placeDetailSegmentController.navigationItem.leftBarButtonItem = leftButton
let btnPost = UIButton(type: .custom)
btnPost.setTitle("POST", for: .normal)
btnPost.addTarget(self, action:#selector(btnPostTapped), for:.touchUpInside)
btnPost.titleLabel?.setFont(Typography.getFont(.PoppinsMedium, size: 12.0), .white)
btnPost.setTitleColor(.menuColor, for: .normal)
btnPost.setTitleColor(.darkGray, for: .highlighted)
btnPost.contentMode = .scaleAspectFit
let rightButton = UIBarButtonItem(customView: btnPost)
placeDetailSegmentController.navigationItem.rightBarButtonItem = rightButton
self.navigationController?.pushViewController(placeDetailSegmentController, animated: true)
如何将键盘从一个视图控制器转移到另一个视图控制器?
答案 0 :(得分:3)
您应按以下步骤更改代码以关闭键盘:
@objc func btnPostTapped() {
AppDelegate.shared.window?.endEditing(true)
}
我们的应用基于window
,因此,当您在窗口上执行任何事件时,它将影响整个应用。我希望这能解决您的问题。
答案 1 :(得分:1)
随时随地隐藏键盘
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil)
或
从导航层次结构中获取所需的viewcontroller实例,并将键盘辞为:
requiredViewController.textField.resignFirstResponder()
或
requiredViewController.view.endEditing(true)