I have a tableview and if a cell is tapped on the tableview, I want to create a UIView to pop up over the tableview and display the contents in the tableview cell. I want to add the code in tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
so that the UIView is opened when I select a row. I just need to know how to create a popup with UIView. I couldn't find any source which helped me out with this. A bit of explanation on how to do this will be awesome. Thanks in advance!
我有一个tableview,如果在tableview上点击一个单元格,我想创建一个UIView来弹出tableview并在tableview单元格中显示内容。我想在tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath)中添加代码,以便在选择行时打开UIView。我只需要知道如何用UIView创建一个弹出窗口。我找不到任何帮助我解决这个问题的来源。关于如何做到这一点的一些解释将是非常棒的。提前致谢!
3 个解决方案
#1
2
Step-1
create one UIView
set the frame as View.bounds
at the same time add the Tap gesture
for hide function.
创建一个UIView将框架设置为View.bounds,同时为隐藏功能添加Tap手势。
Step-2
for present the view on click, use
目前关于点击,使用的观点
UIView.animateWithDuration(1.0, animations: {
yourView.alpha = 1.0
})
Step-3
on hide
UIView.animateWithDuration(1.0, animations: {
yourView.alpha = 0.0
})
finally if you want to hide use hidden
or removefromsuperview
最后如果你想隐藏使用隐藏或removefromsuperview
#2
2
If you are using IOS 8 or later you can just create UIView in the new UIViewController with setting attribute ModalPresentationStyle with value UIModalPresentationStyle. Something like this in you didSelectRowAtIndexPath:
如果您使用的是IOS 8或更高版本,您可以在新的UIViewController中创建UIView,其设置属性ModalPresentationStyle的值为UIModalPresentationStyle。在你做过类似的事情.SelectRowAtIndexPath:
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(500,600)
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
#3
2
First add let vw = UIView()
, then try this
首先添加let vw = UIView(),然后尝试这个
vw.frame = CGRectMake(150, 150, 0, 0)
vw.backgroundColor = UIColor.redColor()
self.view.addSubview(vw)
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.vw.frame = CGRectMake(75, 75, 300, 300)
}, completion: nil)
First create a UIView, and set its X and Y point(150), then width and height (300). I just simply add a background color to show that it is really there. Then the animatedWithDuration
part is to reset the frame so it looks like pop up.
首先创建一个UIView,并设置其X和Y点(150),然后设置宽度和高度(300)。我只是添加一个背景颜色来表明它确实存在。然后animatedWithDuration部分重置帧,使其看起来像弹出。
add this if you want the view behind become darker.
如果您希望后面的视图变得更暗,请添加此项。
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
As for touching background view to close the pop up view. You need a tap gesture recogniser, and function for it
至于触摸背景视图以关闭弹出视图。您需要一个轻敲手势识别器,并为其功能
var tap = UITapGestureRecognizer()
tap.delegate = self
tap.addTarget(self, action: "tapped")
self.view.addGestureRecognizer(tap)
then for its function
那么它的功能
func tapped(){
vw.removeFromSuperview()
}
#1
2
Step-1
create one UIView
set the frame as View.bounds
at the same time add the Tap gesture
for hide function.
创建一个UIView将框架设置为View.bounds,同时为隐藏功能添加Tap手势。
Step-2
for present the view on click, use
目前关于点击,使用的观点
UIView.animateWithDuration(1.0, animations: {
yourView.alpha = 1.0
})
Step-3
on hide
UIView.animateWithDuration(1.0, animations: {
yourView.alpha = 0.0
})
finally if you want to hide use hidden
or removefromsuperview
最后如果你想隐藏使用隐藏或removefromsuperview
#2
2
If you are using IOS 8 or later you can just create UIView in the new UIViewController with setting attribute ModalPresentationStyle with value UIModalPresentationStyle. Something like this in you didSelectRowAtIndexPath:
如果您使用的是IOS 8或更高版本,您可以在新的UIViewController中创建UIView,其设置属性ModalPresentationStyle的值为UIModalPresentationStyle。在你做过类似的事情.SelectRowAtIndexPath:
var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NewCategory") as UIViewController
var nav = UINavigationController(rootViewController: popoverContent)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
var popover = nav.popoverPresentationController
popoverContent.preferredContentSize = CGSizeMake(500,600)
popover.delegate = self
popover.sourceView = self.view
popover.sourceRect = CGRectMake(100,100,0,0)
self.presentViewController(nav, animated: true, completion: nil)
#3
2
First add let vw = UIView()
, then try this
首先添加let vw = UIView(),然后尝试这个
vw.frame = CGRectMake(150, 150, 0, 0)
vw.backgroundColor = UIColor.redColor()
self.view.addSubview(vw)
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
self.vw.frame = CGRectMake(75, 75, 300, 300)
}, completion: nil)
First create a UIView, and set its X and Y point(150), then width and height (300). I just simply add a background color to show that it is really there. Then the animatedWithDuration
part is to reset the frame so it looks like pop up.
首先创建一个UIView,并设置其X和Y点(150),然后设置宽度和高度(300)。我只是添加一个背景颜色来表明它确实存在。然后animatedWithDuration部分重置帧,使其看起来像弹出。
add this if you want the view behind become darker.
如果您希望后面的视图变得更暗,请添加此项。
view.backgroundColor = UIColor(white: 1, alpha: 0.5)
As for touching background view to close the pop up view. You need a tap gesture recogniser, and function for it
至于触摸背景视图以关闭弹出视图。您需要一个轻敲手势识别器,并为其功能
var tap = UITapGestureRecognizer()
tap.delegate = self
tap.addTarget(self, action: "tapped")
self.view.addGestureRecognizer(tap)
then for its function
那么它的功能
func tapped(){
vw.removeFromSuperview()
}