【问题标题】:Alignment issue of UIElements in iOS 7iOS 7 中 UIElements 的对齐问题
【发布时间】:2015-07-10 11:52:19
【问题描述】:

我在 Code 中编写了一个 UITableView 单元格。

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

【问题讨论】:

  • 你使用storyboards还是xib文件?
  • 我在该故事板中使用故事板,我使用的是用代码编写的自定义 tableViewCell

标签: ios iphone swift uitableview xcode6


【解决方案1】:

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

1) 转到情节提要。

2)选择单元格

3)转到尺寸检查器标签

4)尝试修改其内容的自动调整大小

【讨论】:

  • 嗨,其实我已经用代码写了tableViewCell,但是tableView在storyboard中。
  • 如何将您的解决方案应用于用代码编写的 tableViewCell。你知道任何其他可以解决我的问题的解决方案
  • 您可以“玩”tableView 的每个元素的 autoresizingMask。您还必须在这里查看文档:developer.apple.com/library/ios/documentation/UIKit/Reference/…
猜你喜欢
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 2013-10-28
  • 1970-01-01
  • 2013-10-13
相关资源
最近更新 更多