---
title: "ios开发--UINavigationController中弹出自定义alert或自定义view"
author: "lpluck08"
date: "2012-12-15T15:08:57Z"
tags: []
categories: ["移动开发"]
slug: "ios-uinavigationcontroller-alert-view"
draft: false
lastmod: "2012-12-15T15:08:57Z"
id: 1748399
url: "https://apluck.iteye.com/blog/1748399"
---

项目中经常遇到需要在UINavigationController中弹出一个自定义的view或loading窗口之类的，刚开始想到的做法是self.view addSubview添加上去，但navigation bar上的按钮还是可用的。然后又想加到\[UIApplication sharedApplication].keyWindow上，这样navigation bar上的按钮就不可点击了，但总觉得喉咙里有苍蝇一样不舒服。

    现在提供一个自己觉得还行的方式：新建一个新的view controller，讲自定义的view或者loading view 添加到该view controller上，然后在UINavigationController的view controller中写如下代码：

```
self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;   // alertViewController背景透明
[self.navigationController presentModalViewController:alertViewController animated:NO];
```

 

    如果需要去掉调用:

```
[alertViewController dismissModalViewControllerAnimated:NO]
// 如果想加动画，可在此处加自定义动画
```