【问题标题】:iPhone Modal View Smaller that the screeniPhone 模态视图比屏幕小
【发布时间】:2010-10-29 06:08:50
【问题描述】:

我正在尝试做一些不应该那么复杂的事情,但我想不通。 我有一个 UIViewController 显示一个 UITableView。我想在用户按下一行时显示一个上下文菜单。我希望这是一个带有标签和按钮的半透明视图。 我可以使用 AlertView,但我想完全控制标签和按钮的格式,并希望使用 Interface Builder。

所以我创建了 250x290 的小视图,将 alpha 设置为 0.75,并创建了一个带有插座的视图控制器来处理不同的用户事件。

现在我想介绍它。 如果我使用 presentModalViewController 会发生两个(不希望的)事情 1)视图覆盖了整个屏幕(但状态栏除外)。 2) 它是半透明的,但我在它“后面”看到的不是父视图,而是应用程序根视图。

我尝试将其添加为子视图,但没有任何反应,所以我做的不对:

RestaurantContextVC* modalViewController = [[[RestaurantContextVC alloc] initWithNibName:@"RestaurantContextView" bundle:nil] autorelease];
[self.view addSubview:modalViewController.view];

有可能做我想做的事吗? 提前致谢。

贡索

【问题讨论】:

  • 嗨贡索!我正在尝试做同样的事情。在您的最终解决方案中,您最终是否将弹出模式视图变小了? (您在下面的帖子表明背景父母仍然互动的问题)。您是否找到了一种方法来禁用与父屏幕的触摸交互,同时保持模态视图更小(除了部分透明度,用户仍然可以看到模态子视图边缘周围的父信息)?圆形矩形对我有用,但我担心用户触摸模态视图并得到意外响应。我希望唯一可能的返回是模式视图上的按钮。

标签: iphone modal-dialog modalviewcontroller


【解决方案1】:

我正在编写类似的代码。我的方法包括.....

  1. 不使用dismissModalViewControllerAnimated 和presentModalViewController:animated。

  2. 在 IB 中设计定制的全尺寸视图。在其 viewDidLoad 消息体中,将背景颜色设置为 clearColor,以便视图上未被控制器覆盖的空间是透明的。

  3. 我在浮动视图的控制器下放置了一个 UIImageView。 UIImageView 包含一个经过 photoshop 处理的图像,该图像具有圆角并且背景设置为透明。此图像视图用作容器。

  4. 我使用 CoreAnimation 以模态视图样式呈现/关闭浮动视图:(FloatingViewController.m)

    -  (void)viewDidLoad
    {
        [super viewDidLoad];
        [self.view setBackgroundColor:[UIColor clearColor]];
    
        [UIView beginAnimations:nil context:nil];
        [self.view setFrame:CGRectMake(0, 480, 320, 480)];
    
        [UIView setAnimationDuration:0.75f];
        [self.view setFrame:CGRectMake(0, 0, 320, 480)];
        [UIView commitAnimations];
    }
    

【讨论】:

  • 如果我将颜色设置为清除,背景看起来是黑色的
【解决方案2】:

旺吉

这就是我找到的解决方案。 我使用 loadNibNamed 加载视图,然后使用 addSubView 将其添加到顶部,如下所示:

//Show a view on top of current view with a wait indicator. This prevents all user interactions.
-(void) showWaitView{
    NSArray* nibViews =  [[NSBundle mainBundle] loadNibNamed:@"WaitView" owner:self options:nil];
#ifdef __IPHONE_2_1
    waitView = [ nibViews objectAtIndex: 0];
#else
    waitView = [ nibViews objectAtIndex: 1];
#endif          
    CGFloat x = self.view.center.x - (waitView.frame.size.width / 2);
    CGFloat y = self.view.center.y - (waitView.frame.size.height / 2);
    [waitView setFrame:CGRectMake(x,y,waitView.bounds.size.width,waitView.bounds.size.height)];
    [self.view addSubview:waitView];    
}

您能否详细说明第 3 点和第 4 点?

为了让视图呈现圆形矩形,我所做的是把它放在一个圆形矩形按钮内。

此代码实际上允许您拥有一个小的浮动视图,但如果该视图小于其父视图,则用户可以与父视图的可见部分进行交互。

最后我创建了相同大小的视图,但保留了代码以防万一。

贡索

【讨论】:

  • waitView 是 UIView 还是 ViewController?我试过你的代码,它告诉我 waitView 没有框架。我也准备了笔尖,但是我不能把笔尖屏幕变成 UIView,只能变成 UIViewController!
【解决方案3】:

我强烈考虑使用导航控制器在您的子视图中滑动而不是覆盖它。这是预期的模型,您可能认为通过自己的方式进行操作会获得的任何小好处都会被(最小)意外原则大大抵消。

如果你真的必须这样做,我相信诀窍是将第一个表视图添加为视图控制器维护的透明“持有”视图的子视图。然后将您的新子视图添加为该子视图的另一个子视图。

【讨论】:

  • 你可以使用导航控制器在父视图上添加一个小视图吗?我想设计一个带有控件的完整视图作为一个小视图并将背景设置为透明?
  • 我也有同样的问题:小视图覆盖可能吗?我也不想用滑入式覆盖整个屏幕,尽管困扰我的不是“滑动”而是全覆盖。我想让父级部分可见,因为除了生成子视图的选项之外,用户还可以从了解其他选项中受益。我也想将背景设置为透明,但在模式视图启动时不启用与后屏幕的交互。模态视图将有一个取消按钮、一个详细的可滑动 UIImageView(在图像之间切换)和一个确认按钮。谢谢!
【解决方案4】:

再一次,如果你真的想这样做,而不是添加一个透明的“持有”视图,因为这个弹出窗口本质上是模态的,我会将它直接设为窗口的子视图。

您可能希望在其后面放置一个透明的黑色盾牌,以防止触摸背景并将输入焦点集中在弹出窗口上。

但说真的,请考虑在堆栈上弹出一个控制器或使用该警报视图。除非你聘请了一个 $$ 的设计师,否则它在 iPhone 上可能看起来不合适。

【讨论】:

    【解决方案5】:

    我所做的是在我的应用委托中的 UINavigation 控制器之上创建一个 UIViewController,并为方便起见将其设为单例对象的属性:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    //--- create root navigation controller
    self.window.rootViewController = self.navigationController;
    
    //--- create view controller for popups:
    popupViewController = [[BaseViewController alloc] init];
    popupViewController.view.backgroundColor = [UIColor clearColor];
    popupViewController.view.hidden = true; //for rendering optimisation
    [self.window addSubview:popupViewController.view];
    [AppState sharedInstance].popupViewController = self.popupViewController;
    
    //--- make all visible:
    [self.window makeKeyAndVisible];
    
    return YES;
    

    }

    然后我可以在我的应用程序中的任何位置调用例如

    MyViewController * myVC = [[UIViewController alloc] init];
    //... set up viewcontroller and its view...
    // add the view of the created view controller to the popup view:
    [AppState sharedInstance].popupViewController.view.hidden = false;
    [[AppState sharedInstance].popupViewController.view addSubview:myVC.view];
    

    上面使用的 BaseViewController 只是继承自 UIViewController 并设置了全屏视图:

    //----- in BaseViewController implementation
    - (void)loadView {
       //------- create root view:
       CGRect frame = [[AppState sharedInstance] getScreenFrame]; 
       rootView = [[VCView alloc] initWithFrame:frame];
       rootView.backgroundColor = [UIColor whiteColor];
       self.view = rootView;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      相关资源
      最近更新 更多