iPhone Modal View Smaller that the screen

一曲冷凌霜 提交于 2019-12-03 03:20:12

问题


I'm trying to do something that shouldn't be that complicated, but I can't figure it out. I have a UIViewController displaying a UITableView. I want to present a context menu when the user press on a row. I want this to be a semi-transparent view with labels and buttons. I could use an AlertView, but I want full control on the format of the labels and buttons and will like to use Interface Builder.

So I created my small view 250x290, set the alpha to .75 and create a view controller with the outlets to handle the different user events.

Now I want to present it. If I use presentModalViewController two (undesired) things happen 1) the view covers all of the screen (but the status bar). 2) It is semi-transparent, but what I see "behind" it its not the parent view but the applications root view.

Ive tried adding it as a subview, but nothing happens, so Im not doing something right:

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

Is it possible to do what I want? Thanks in advance.

Gonso


回答1:


I'm coding similar thing. My approach include.....

  1. Not using dismissModalViewControllerAnimated and presentModalViewController:animated.

  2. Design a customized full sized view in IB. In its viewDidLoad message body, set the background color to clearColor, so that space on the view not covered by controllers are transparent.

  3. I put a UIImageView under the controllers of the floating view. The UIImageView contains a photoshoped image, which has rounded corners and the background is set to transparent. This image view serves as the container.

  4. I uses CoreAnimation to present/dismiss the floating view in the modal view style: (the 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:


wangii

Thats pretty much the solution I found. I load the view with loadNibNamed and then just add it on top with addSubView, like this:

//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];    
}

Could you elaborate on points 3 and 4?

What I did to give the view the round rect aspect is put it inside a round rect button.

This code will actually allow you to have a small floating view, but if the view is smaller that its parent, the user could interact with the visible part of the parent.

In the end I create my view with the same size, but kept the code just in case.

Gonso




回答3:


I would strongly consider using a navigation controller to slide in your subview instead of overlaying it. This is the expected model and any small benefit you may think you'll get by doing it your own way will be greatly offset by the principle of (least) surprise.

If you really really have to do it this way, I believe the trick is to add the first table view as a subview of a transparent "holding" view that the view controller maintains. Then add your new sub view as another subview of that.




回答4:


Again, if you really want to do this, instead of adding a transparent "holding" view, since this pop-up is essentially modal, I would make it a subview directly of the window.

You might want to put in a transparent black shield behind it to prevent touches on the background and focus input on the popup.

But seriously, consider either popping a controller on the stack or using that alert view. Unless you've hired a $$ designer, it's probably not going to look appropriate on the iPhone.




回答5:


What I did was create a UIViewController on top of my UINavigation controller in my app delegate and made it a property of a singleton object for convenience:

- (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;

}

At any point in my app, I can then call e.g.

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];

The BaseViewController used on the top just inherits from UIViewController and sets up a full-screen view:

//----- 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;
}


来源:https://stackoverflow.com/questions/915318/iphone-modal-view-smaller-that-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!