Ios 如果屏幕相对较小,UITableViewCell按钮将消失

Ios 如果屏幕相对较小,UITableViewCell按钮将消失,ios,swift,uitableview,Ios,Swift,Uitableview,我正在使用Swift 5和Xcode 11创建一个简单的选项卡式(3个选项卡栏项目)应用程序。在iOS应用程序中,最初有两个标签栏项目,我想为我的应用程序制作一些主题,于是我就这样做了,在我的应用程序中再添加一个标签栏项目。我使用UITableViewCells和编程制作的按钮添加了供用户选择的主题,如下所示: 这就是看起来的样子。至少对于大屏幕的iPhone来说是这样。从iPhone6开始,一切正常,如上图所示。但在iPhone 6下面,就像这部iPhone 5一样,左侧的按钮组消失了:

我正在使用Swift 5和Xcode 11创建一个简单的选项卡式(3个选项卡栏项目)应用程序。在iOS应用程序中,最初有两个标签栏项目,我想为我的应用程序制作一些主题,于是我就这样做了,在我的应用程序中再添加一个标签栏项目。我使用UITableViewCells和编程制作的按钮添加了供用户选择的主题,如下所示:

这就是看起来的样子。至少对于大屏幕的iPhone来说是这样。从iPhone6开始,一切正常,如上图所示。但在iPhone 6下面,就像这部iPhone 5一样,左侧的按钮组消失了:

这很奇怪,我不知道为什么正确的按钮会留下来

这是我的UITableViewController:

导入UIKit
@ObjectMembers类CustomViewController:UITableViewController{
重写func viewDidLoad(){
super.viewDidLoad()
}
覆盖函数视图将出现(uo动画:Bool){
self.tableView.rowHeight=138
}
重写func tableView(tableView:UITableView,标题或标题部分
节:Int)->字符串{
返回“主题”
}
重写func numberOfSections(在tableView:UITableView中)->Int{
//#警告未完成执行,返回节数
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回SingletonViewController.themes.count
}
// 3
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“themeCell”,表示:indexPath)为!themeCell
var cellyi=ui按钮(帧:CGRect(x:5,y:5,宽度:50,高度:30))
cellyi.tag=indexPath.row+1
/////////
如果cellyi.tag==1{
让cellButton1=UIButton(帧:CGRect(x:0,y:5,宽度:88,高度:119.5))
cellButton1.TranslatesAutoResizengMacSkinToConstraints=false
cell.addSubview(cellButton1)
cell.accessoryView=cellButton1
cellButton1.leadingAnchor.constraint(等式:cell.leadingAnchor,常数:10)。isActive=true
cellButton1.topAnchor.constraint(等式:cell.topAnchor,常量:10)。isActive=true
cellButton1.widthAnchor.constraint(equalToConstant:88)。isActive=true
cellButton1.heightAnchor.constraint(equalToConstant:119.5)。isActive=true
cellButton1.addTarget(self,action:#选择器(CustomViewController.backBTN(发送方:)),for:.touchUpInside)
cellButton1.setTitle(“经典”,用于:。正常)
cellButton1.setTitleColor(UIColor.black,表示:。正常)
cellButton1.tag=indexPath.row+1
}否则{
让cellButton=UIButton(帧:CGRect(x:0,y:5,宽度:88,高度:119.5))
cellButton.TranslatesAutoResizezingMaskintoConstraints=false
cell.addSubview(cellButton)
cell.accessoryView=cellButton
cellButton.leadingAnchor.constraint(等式:cell.leadingAnchor,常数:10)。isActive=true
cellButton.topAnchor.constraint(equalTo:cell.topAnchor,常量:10)。isActive=true
cellButton.widthAnchor.constraint(equalToConstant:88).isActive=true
cellButton.heightAnchor.constraint(equalToConstant:119.5).isActive=true
tag=indexPath.row-1
cellButton.setImage(UIImage(名为:SingletonViewController.themes[indexPath.row-1]),用于:UIControl.State.normal)
cellButton.addTarget(self,action:#选择器(CustomViewController.backBTN(发送方:)),for:.touchUpInside)
cellButton.tag=indexPath.row+1
}
//////////
cell.addSubview(cellyi)
cell.accessoryView=cellyi
cellyi.backgroundColor=UIColor.red
cellyi.addTarget(self,action:#选择器(CustomViewController.backBTN(发送方:)),for:.touchUpInside)
如果UserDefaults.standard.integer(forKey:“like”)==0{
UserDefaults.standard.set(1,forKey:“like”)
}
如果UserDefaults.standard.integer(forKey:“like”)==indexPath.row+1{
cellyi.backgroundColor=UIColor.green
如果indexath.row+1==1{
self.tabBar控制器?.tabBar.barTintColor=UIColor(红色:0.84,绿色:0.84,蓝色:0.84,阿尔法:1.0)
self.tabBar控制器?.tabBar.tintColor=UIColor(红色:0.21,绿色:0.56,蓝色:0.96,阿尔法:1.0)
}如果indexath.row+1==2,则为else{
self.tabBar控制器?.tabBar.barTintColor=UIColor.black
self.tabBar控制器?.tabBar.tintColor=UIColor(红色:0.98,绿色:0.64,蓝色:0.02,阿尔法:1.0)
}
}
tableView.AllowSelection=false
返回单元
}
@objc func backBTN(发送方:UIButton){
UserDefaults.standard.set(sender.tag,forKey:“like”)
tableView.reloadData()
}
}

我看过其他一些类似的问题。目前,我还没有找到解决这个问题的办法。非常感谢您的帮助

我确信您不需要在单元格中动态添加按钮。使用按钮创建单元原型,使用自动布局,并使用自定义单元类自定义单元。请参阅中的答案,我确信您不需要在单元格中动态添加按钮。使用按钮创建单元原型,使用自动布局,并使用自定义单元类自定义单元。请参阅中的答案