iPhone Modal View Smaller that the screen

后端 未结 5 1043
一生所求
一生所求 2021-01-31 06:23

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

相关标签:
5条回答
  • 2021-01-31 06:50

    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;
    }
    
    0 讨论(0)
  • 2021-01-31 06:54

    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.

    0 讨论(0)
  • 2021-01-31 06:57

    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.

    0 讨论(0)
  • 2021-01-31 07:01

    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

    0 讨论(0)
  • 2021-01-31 07:08

    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];
      }
      
    0 讨论(0)
提交回复
热议问题