当设备改变方向时,UITableView中的图像会增大和缩小

时间:2016-02-02 15:13:43

标签: ios iphone uitableview orientation

我在我的应用程序中遇到了我觉得奇怪的怪癖......

我有一个UITableView,每个部分都有几个部分和行。在每个标题的最右边是绿色+点击以在下面添加新行。在每行的最右侧是一个铅笔按钮,用于点击以编辑行。工作得很漂亮。

然而,当我旋转设备时,铅笔按钮暂时增长到其大小的3倍左右,并且当它占据屏幕边缘的新位置时会缩小。在改变横向和纵向时都会发生这种情况。我已经包含了照片给你看,但在旋转过程中很难获得一个屏幕截图......但绿色+按钮不会发生这种情况。

我的UITableView的代码也在下面。这很漫长而令人费解 - 抱歉。只是希望有人能指出为什么会这样。谢谢!

普通铅笔 enter image description here

inflated pencil enter image description here

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {    
    return 0.00001
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 35
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
    let headerBar = UITableViewHeaderFooterView(frame: CGRect(x: 0, y: 0, width: tablePrayer.frame.width, height: 35))

    let hLabel = UILabel()
    hLabel.text = prayerListSectionNames[section].uppercaseString
    hLabel.frame = CGRectMake(25, 0, headerBar.frame.width - 35, 35)
    hLabel.font = UIFont.boldSystemFontOfSize(14)
    hLabel.textAlignment = .Left    
    headerBar.addSubview(hLabel)

    let headerButton = UIButton()

    let imgX = UIImageView(image: UIImage(named:"red white x.png"))
    imgX.frame = CGRectMake(2, 10, 15, 15)
    imgX.contentMode = .ScaleAspectFit

    let imgP = UIImageView(image: UIImage(named:"green plus.png"))
    imgP.frame = CGRectMake(2,10,15,15)
    imgP.contentMode = .ScaleAspectFit

    showPrayerListSection = NSUserDefaults.standardUserDefaults().objectForKey("showPrayerListSection") as! Array
    var arrow = UIImage(named: "exp clps arrow right.png")

    if showPrayerListSection[section] {    
        arrow = UIImage(named: "exp clps arrow down.png")!
    }

    let imgExpand = UIImageView(image: arrow)
    imgExpand.frame = CGRectMake(10, 0, 10, 35)
    imgExpand.contentMode = .ScaleAspectFit
    headerBar.addSubview(imgExpand)

    headerButton.frame = CGRectMake(tablePrayer.frame.width - 30, 0, 20, 35)
    headerButton.tag = 20000 + section

    if movingRows {
        headerButton.addSubview(imgX)
        headerButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: .TouchUpInside)
    } else {
        headerButton.addSubview(imgP)
        headerButton.addTarget(self, action: "addButtonPressed:", forControlEvents: .TouchUpInside)
    }
    headerBar.addSubview(headerButton)

    let toggleShowButton = UIButton()
    toggleShowButton.frame = CGRectMake(0, 0, tablePrayer.frame.width - 50, 35)
    toggleShowButton.addTarget(self, action: "toggleShowPressed:", forControlEvents: .TouchUpInside)
    toggleShowButton.tag = 30000 + section
    headerBar.addSubview(toggleShowButton)
return headerBar
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    var numberOfSections = 0

    if NSUserDefaults.standardUserDefaults().objectForKey("prayerListSectionNames") != nil {    
        prayerListSectionNames = NSUserDefaults.standardUserDefaults().objectForKey("prayerListSectionNames") as! Array
        numberOfSections = prayerListSectionNames.count
    }

    return numberOfSections
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {    
    var rowsInSection = 0
    var tempNames = [String]()

    showPrayerListSection = NSUserDefaults.standardUserDefaults().objectForKey("showPrayerListSection") as! Array

    if showPrayerListSection[section] {
        if NSUserDefaults.standardUserDefaults().objectForKey(prayerListSectionNames[section]) != nil {  
            tempNames = NSUserDefaults.standardUserDefaults().objectForKey(prayerListSectionNames[section]) as! Array
            rowsInSection = tempNames.count
        }
    }

    return rowsInSection
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        
    var sectionItems = [String]()

    if NSUserDefaults.standardUserDefaults().objectForKey(prayerListSectionNames[indexPath.section]) != nil {
        sectionItems = NSUserDefaults.standardUserDefaults().objectForKey(prayerListSectionNames[indexPath.section]) as! Array
    }

    let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "prayerCell")
    cell.textLabel?.text = sectionItems[indexPath.row]
    cell.textLabel?.font = UIFont.systemFontOfSize(12)
    cell.textLabel?.numberOfLines = 0

    let imgPencil = UIImageView(image: UIImage(named: "pencil.png"))
    imgPencil.frame = CGRectMake(7, 0, 20, cell.frame.height)
    imgPencil.autoresizingMask = UIViewAutoresizing.FlexibleHeight
    imgPencil.contentMode = .ScaleAspectFit

    let pencilButton = UIButton()

    let dbX = tableView.frame.width - 35
    pencilButton.frame = CGRectMake(dbX, 0, 30, cell.frame.height)
    pencilButton.autoresizingMask = UIViewAutoresizing.FlexibleHeight

    if !movingRows {
        pencilButton.addSubview(imgPencil)
    }

    pencilButton.titleLabel?.textColor = UIColor.clearColor()
    pencilButton.titleLabel!.text = "\(indexPath.section)"
    pencilButton.tag = 100000 + indexPath.row
    pencilButton.addTarget(self, action: "pencilButtonPressed:", forControlEvents: .TouchUpInside)

    cell.addSubview(pencilButton)

    return cell 
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    tablePrayer.reloadData()
}

1 个答案:

答案 0 :(得分:0)

这是因为你动态分配了按钮的宽度。你取屏幕的宽度 - 35px。当屏幕旋转时,屏幕的宽度会发生变化,所以你按下了。

您应该检查您的屏幕是纵向还是横向,根据您应该为按钮宽度和高度给出适当的值。