2015-07-10 120 views
0

我在代码中编写了一个UITableView单元。UIElements在iOS 7中的对齐问题

override init(style: UITableViewCellStyle, reuseIdentifier: String?) 
{ 

super.init(style: style, reuseIdentifier: reuseIdentifier) 

     var yPos : CGFloat = 10.0 

     let collectionFrame = CGRectMake(0, yPos, Constants.DimentionKeys.kDeviceWidth, Constants.DimentionKeys.kDeviceWidth) 
     var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.scrollDirection = UICollectionViewScrollDirection.Horizontal 
     layout.itemSize = CGSizeMake(Constants.DimentionKeys.kDeviceWidth, Constants.DimentionKeys.kDeviceWidth) 
     layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) 
     layout.minimumInteritemSpacing = 0 
     layout.minimumLineSpacing = 0 

     self.ImageCollectionView = UICollectionView(frame: collectionFrame, collectionViewLayout: layout) 
     self.ImageCollectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell") 
     self.ImageCollectionView?.tag = 11 
     self.ImageCollectionView?.pagingEnabled = true 
     self.ImageCollectionView?.backgroundColor = UIColor(red: 242/255.0, green: 237/255.0, blue: 235/255.0, alpha: 1.0) 

     self.contentView.addSubview(self.ImageCollectionView!) 



     self.ShareBtn.frame = CGRectMake(10.0, yPos, 35.0, 35.0) 
     self.ShareBtn.setImage(UIImage(named: "share"), forState: UIControlState.Normal) 
     self.contentView.addSubview(self.ShareBtn) 

     yPos += collectionFrame.height 

     self.offerLbl.frame = CGRectMake(10.0, yPos, Constants.DimentionKeys.kDeviceWidth - 20.0, 30.0) 
     self.offerLbl.numberOfLines = 3 
     self.offerLbl.textAlignment = NSTextAlignment.Center 
     self.offerLbl.font = UIFont(name: "Helvetica Neue", size: 12.0) 
     self.offerLbl.backgroundColor = UIColor(red: 213/255.0, green: 221/255.0, blue: 234/255.0, alpha: 1.0) 
     self.contentView.addSubview(self.offerLbl) 

     yPos += 45.0 

     self.priceLbl.frame = CGRectMake(10.0, yPos, self.offerLbl.frame.size.width - 35.0, 35.0) 
     self.priceLbl.font = UIFont(name: "Verdana", size: 21.0) 
     self.contentView.addSubview(self.priceLbl) 

     self.wishListBtn.frame = CGRectMake(self.priceLbl.frame.size.width + 10.0, yPos, 35.0, 35.0) 
     self.wishListBtn.setImage(UIImage(named: "wishlist"), forState: UIControlState.Normal) 
     self.contentView.addSubview(self.wishListBtn) 

     yPos += 40.0 

     self.splPriceLbl.frame = CGRectMake(10.0, yPos, self.priceLbl.frame.width, 20.0) 
     self.splPriceLbl.font = UIFont(name: "Verdana", size: 14.0) 
     self.splPriceLbl.textColor = UIColor.redColor() 
     self.contentView.addSubview(self.splPriceLbl) 

     yPos += 25.0 


     self.productNameLbl.frame = CGRectMake(10.0, yPos, self.frame.width - 20.0, 20.0) 
     self.productNameLbl.font = UIFont(name: "Helvetica Neue", size: 16.0) 
     self.contentView.addSubview(self.productNameLbl) 

     yPos += 22.0 

     self.productDescLbl.frame = CGRectMake(10.0, yPos, self.frame.width - 20.0, 40.0) 
     self.productDescLbl.font = UIFont(name: "Helvetica Neue", size: 13.0) 
     self.productDescLbl.numberOfLines = 5 
     self.contentView.addSubview(self.productDescLbl) 

     yPos += 45.0 


     self.variantSelectorBtn.frame = CGRectMake(10.0, yPos, (Constants.DimentionKeys.kDeviceWidth/2.0 - 20.0), 35.0) 
     self.variantSelectorBtn.setTitle("Customise this product", forState: UIControlState.Normal) 
     self.variantSelectorBtn.titleLabel?.font = UIFont.systemFontOfSize(12.0) 
     self.variantSelectorBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) 
     self.variantSelectorBtn.layer.borderWidth = 1.0 
     self.variantSelectorBtn.layer.borderColor = UIColor.blackColor().CGColor 
     self.contentView.addSubview(self.variantSelectorBtn) 


     self.TBtn.frame = CGRectMake(Constants.DimentionKeys.kDeviceWidth/2.0 + 20.0, yPos, Constants.DimentionKeys.kDeviceWidth/2.0 - 40.0, 35.0) 
     self.TBtn.setAttributedTitle(Product.getTwoColorString("Free Try"), forState: UIControlState.Normal) 
     self.TBtn.titleLabel?.font = UIFont.systemFontOfSize(12.0) 
     self.TBtn.layer.borderWidth = 1.0 
     self.TBtn.layer.borderColor = UIColor.blackColor().CGColor 
     self.contentView.addSubview(self.TBtn) 

     yPos += 35.0 

    } 

该UITableView单元中的所有UIElemens在执行时向左移动。即使对于X位置为零的UIElements(该UIElement的某些部分移出屏幕左侧),这仅在iOS 7中发生。我正在使用的应用程序支持从iOS 7到iOS 8.3,它是一个通用应用程序。

+0

你用故事板或XIB文件? – hoya21

+0

我在故事板中使用故事板我正在使用自定义tableViewCell写在代码 –

回答

0

也许你必须检查单元格及其内容的自动调整值。

1)转到故事板。

2)选择单元格

3)转到尺寸检查标签

4)尝试修改自动尺寸值,其内容

+0

嗨,其实我已经在代码中编写tableViewCell,但tableView是故事板。 –

+0

如何将您的解决方案应用于用代码编写的tableViewCell。你知道任何其他解决方案,可以解决我的问题 –

+0

你可以用你的tableView的每个元素的autoresizingMask“玩”。你也必须在这里查看文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/instp/UIView/autoresizingMask – hoya21