我正在尝试在模态视图的顶部添加“X”按钮,因此如果用户按下该按钮,则会关闭模态视图。我查看了这里提出的解决方案how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
但我在这里有两个跟进问题,因为我对ios编程还不熟悉
上页提出的解决方案似乎对我不起作用。我在模态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];
我看到的是
提前感谢您的帮助
答案 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的触摸区域。