iPhone下弹出视图的方法

本文介绍了在iOS环境下通过增加UIWindow和背景视图的方法来实现局部弹出视图的功能,详细阐述了两种方案的具体实现步骤,包括窗口的创建、背景视图的响应消息处理以及视图的显示与隐藏过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


iPhone 下不支持 UIPopoverController,而使用 presentViewController:animated:completion: 方法得到的效果会全屏显示。有时候我们只想在部分区域类弹出视图,点击视图外区域自动消失。这里提供两种方案。


方案1:

1)增加 UIWindow,在新的窗口中添加视图控制器。

window =UIWindow(frame: UIScreen.mainScreen().bounds)

        window.windowLevel =UIWindowLevelNormal

        window.backgroundColor =UIColor.clearColor()

        window.rootViewController =FakeViewController()


2)增加背景视图,用于响应消息

        dimBackground = UIView(frame: UIScreen.mainScreen().bounds

        dimBackground.backgroundColor =UIColor.clearColor()           

        let gr:UITapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("dismiss"))  

        dimBackground.addGestureRecognizer(gr)  

        window.rootViewController!.view.addSubview(dimBackground)

            

3)创建actionSheet视图(这里省略),用于显示,为了得到移动的动画效果,先将其移出屏幕

        var tmpFrame: CGRect = self.actionSheet.frame

        tmpFrame.origin.y =UIScreen.mainScreen().bounds.size.height

        actionSheet.frame = tmpFrame;

        window.rootViewController!.view.addSubview(actionSheet)

           

4)从下往上移动到屏幕之内

        self.window.hidden =false

        UIView.animateWithDuration(0.2, animations:{

            self.dimBackground.backgroundColor =UIColor(white: 0.0, alpha:0.2)

            let frame: CGRect = UIScreen.mainScreen().bounds

            self.actionSheet.frame =CGRectMake(0, frame.size.height-self.actionSheet.frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);

        })

 

5)点击背景视图响应消息,销毁窗口(具体可根据要求,或者保存,下次需要再次移到屏幕内即可)

func dismiss() {

            UIView.animateWithDuration(0.2, animations: {[weak weakSelf =self] () -> Voidin

                self.dimBackground.backgroundColor =UIColor.clearColor()

                let frame: CGRect = UIScreen.mainScreen().bounds

                self.actionSheet.frame =CGRectMake(0, frame.size.height,self.actionSheet.frame.size.width,self.actionSheet.frame.size.height);

            }) { (complete) -> Void in

                self.window =nil

            }

        }




方案2:

1)创建要显示的 UIViewController

var frame:CGRect = self.view.bounds

        frame.origin.y = frame.size.height - 44.0

        frame.size.height =44.0

       let pageJumpController = PageJumpController(frame: frame, totolPage: (currentPageItem!.totalPage), delegate:self)

            

2)加入视图到上层视图上,并将视图控制器加入其栈中

       self.view.addSubview(pageJumpController.view)

       self.addChildViewController(pageJumpController);

        pageJumpController.didMoveToParentViewController(self)

            

3)不想显示时,移出栈即可。注意,如果只将子视图移除,其控制器仍然在栈中,被父控制器所有,不会被销毁。根据具体情况实现

self.view.removeFromSuperview()

        self.removeFromParentViewController()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值