将关闭按钮添加到部分偏离视图的模态视图

时间:2013-07-29 18:48:46

标签: ios objective-c uiview modal-dialog

我正在尝试在模态视图的顶部添加“X”按钮,因此如果用户按下该按钮,则会关闭模态视图。我查看了这里提出的解决方案how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?

modal view with 'x' button

但我在这里有两个跟进问题,因为我对ios编程还不熟悉

  1. 上页提出的解决方案似乎对我不起作用。我在模态VC的viewDidLoad中首先使用普通按钮尝试了它,我看到它只出现在模态视图的范围内,而不是我想要的外部。

    UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    buttonView.backgroundColor = [UIColor brownColor];
    CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
    UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
    [button setTitle: @"Close" forState: UIControlStateNormal];
    [button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
    [button addTarget:self
                action:@selector(closeButtonPressed)
      forControlEvents:UIControlEventTouchUpInside];
    [buttonView addSubview: button];
    
    
    CGRect parentView = self.view.superview.frame;
    CGRect currentView = self.view.frame;
    CGRect buttonViewFrame = buttonView.frame;
    buttonViewFrame.origin = CGPointMake(parentView.origin.x -      buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
    
    [buttonView setFrame:buttonViewFrame];
    [self.view addSubview:buttonView];
    
  2. 我看到的是clipped image

    1. 如何绘制“X”按钮我是否使用CGRect绘制它,或者在上面的示例中添加一个按钮而不是添加一个按钮我应该添加一个带有“X”的图像作为子视图?
    2. 提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

这样做,希望帮助你:)


    //adding button to your view 
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
    aView.backgroundColor = [UIColor blueColor];
    aView.tag = 100;
   [self.view addSubview:aView];

   UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [aButton setTitle:@"CLOSE" forState:UIControlStateNormal];
   [aButton addTarget:self action:@selector(tapped:)     forControlEvents:UIControlEventTouchUpInside];
   aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds 
   aButton.backgroundColor = [UIColor redColor];
   [aView addSubview:aButton];
   [aView release];     //i am doing without ARC


答案 1 :(得分:0)

另一个选项是创建一个更大的视图,并将您的UITableView与关闭按钮放在一起。然后,关闭按钮可以位于(0,0)位置,UITableView可以从(25,25)开始。 这可以防止并发症 - 我看到剪切视图无法识别主视图外部的触摸,因此您的50x50按钮仅具有25x25的触摸区域。