Ios 不能在Swift上居中
我正在尝试制作一个popover,允许用户在图像之间滑动(有没有比使用popover更好的方法?) 现在为了制作popover,我花了几个小时在谷歌上搜索如何在屏幕上制作一个矩形中心。从互联网上,我的代码是:Ios 不能在Swift上居中,ios,swift,uipopovercontroller,Ios,Swift,Uipopovercontroller,我正在尝试制作一个popover,允许用户在图像之间滑动(有没有比使用popover更好的方法?) 现在为了制作popover,我花了几个小时在谷歌上搜索如何在屏幕上制作一个矩形中心。从互联网上,我的代码是: // get a reference to the view controller for the popover let popController = UIStoryboard(name: "Event", bundle: nil).instantiateViewCon
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Event", bundle: nil).instantiateViewController(withIdentifier: "carouselPopover")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
let width = view.frame.width
popController.preferredContentSize = CGSize(width: width, height: 300)
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = self.view
popController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
// present the popover
self.present(popController, animated: true, completion: nil)
然而,就我的一生而言,我无法理解为什么府绸没有居中
当我设置popController的首选内容大小时,它只会失去中心。有什么想法吗
TL:DR
我想1)使popover在屏幕上居中,2)使popover的比例为1:1,3)使popover的宽度与父屏幕的宽度成比例。如果没有1000行代码,我怎么做呢。您可以创建一个自定义的popover类。然后建立代理模式以确定用户是否刷卡
protocol PopoverDelegate: class {
func imageviewDidSwipe(_ popover: Popover)
}
class Popover: UIView {
weak var delegate: PopoverDelegate?
init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.white
}
func setupImage(_ image: UIImage) {
let imageView = UIView(frame: CGRect.zero)
imageView.image = image
self.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
imageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
imageView.heightAnchor.constraint(equalToConstant: 50).isActive = true // However big you want
imageView.widthAnchor.constraint(equalToConstant: 50).isActive = true // However big you want
}
func showPopover(over view: UIView) {
view.addSubview(self)
translatesAutoResizingMaskIntoConstraints = false
centerXAnchor.contraint(equalTo: view.centerXAnchor).isActive = true
centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
heightAnchor.constraint(equalToConstant: frame.height).isActive = true
widthAnchor.constraint(equalToConstant: frame.width).isActive = true
}
}
使用
class vC: UIViewController {
func ImageOnClick() {
// change view to where you want popover to show on top of
let popover = Popover(frame: view.frame)
popover.setupImage(image.png)
popover.delegate = self
showPopover(over: view)
}
}
extension vC: PopoverDelegate {
func imageviewDidSwipe(_ popover: Popover) {
// image swiped
}
}
什么是-
let width=view.frame.width
打印出来?什么是视图,什么是框架?为什么不使用自动布局?根据规则完成您的问题,它缺少信息我如何使用popover的autolayout?frame width是调用popover的父帧的宽度。为什么不使用UICollectionView并启用滚动方向=水平
和分页
。启用分页功能后,它将始终位于中心位置。再说一次,我不是问滚动,我是问如何将popover居中。@AlexKornhauser我不知道popover的机制,但您可以通过自定义视图和默认动画实现所需的行为。先别管滑动了,我使用的是外部库。我想1)使popover居中,2)使布局为1:1,3)使布局相对于屏幕的宽度。UIPopopOvercontroller类已被弃用,因此我建议创建一个customclass。对不起,我现在不在电脑旁,无法回答这些问题。