ios - 如何将UIButton作为bottomView添加到UIView?

标签 ios objective-c uiview uibutton

我有一个 UIView 类,在 xib 中我也有一个 MainView,它是一个 UIView。我想将 UIButton 作为 BottomView 添加到 View 中。我需要在 MainView 和 BottomView 之间提供底部空间 16。我该怎么做?

 self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
    [self.bottomView setBackgroundColor:[UIColor greenColor]];
self.selectBtn= [UIButton buttonWithType: UIButtonTypeRoundedRect];
[self.selectBtn setFrame:CGRectMake(0,20,100,40)];
[self.selectBtn setTitle:@"Select" forState:UIControlStateNormal];
[self.selectBtn addTarget:self action:@selector(selectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.selectBtn];
    [self addSubview:self.bottomView];

Expected result

Output from above code

最佳答案

请发现您在此框架上添加了按钮

 self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];

在这里,您将 y 设置为高度 + 30,因此它在 View 中不可见。

你应该使用这个。

 self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height - 40, self.MainView.bounds.size.width, 40.0)];

此外,如果您想要 16 像素间隙,您应该使用相同的方法在其上方的 View 。

如果您需要更多帮助,请告诉我

关于ios - 如何将UIButton作为bottomView添加到UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56852222/

相关文章:

ios - 类型 "GameScene"不符合协议(protocol)“SKPaymentTransactionObserver

ios - 无法识别的选择器发送到应用内购买产品的实例错误

ios - 显示加载 View

ios - 在 touchesMoved 中调用 hitTest

ios - UIView填充颜色

ios - 重新加载应用程序,就像它在进入前台时被杀死一样

ios - 在 Swift 4 中升级后未调用 textFieldShouldReturn

ios - UICollectionView:删除单元格之间的边距

objective-c - 用字符串数组填充 UITableView

objective-c - 通过 Casting 调用另一个类中的方法?