0

我正在尝试在模态视图的顶部添加一个“X”按钮,因此如果用户按下它会关闭模态视图。我查看了此处提出的解决方案如何将关闭按钮添加到 UIModalPresentationPageSheet 中显示的模态视图角?

带有“x”按钮的模态视图

但是我在这里有两个后续问题,因为我对 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];
    

我看到的是剪裁的图像

  1. 如何绘制“X”按钮,我应该使用 CGRect 来绘制它还是在上面的示例中添加一个按钮而不是添加一个带有“X”的图像作为子视图?

在此先感谢您的帮助

4

2 回答 2

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


于 2013-07-30T04:47:22.667 回答
0

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

于 2013-07-30T07:09:23.030 回答