首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当设备改变方向时,UITableView中的图像会增大和缩小

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

Stack Overflow用户
提问于 2016-02-02 23:13:43
回答 1查看 249关注 0票数 0

我在我的应用程序中遇到了一个奇怪的怪事...

我有一个UITableView,它有几个部分,每个部分都有行。在每个标题的最右边是一个绿色的+,点击即可在下方添加新行。在每一行的最右边是一个铅笔按钮,轻触即可编辑该行。效果很好。

然而,当我旋转设备时,铅笔按钮暂时增大到大约3倍大小,并随着它占据屏幕边缘的新位置而缩小。这在改变风景和肖像的时候都会发生。我已经添加了一些照片给你看,但在旋转过程中很难截取屏幕截图……然而,这并没有发生在绿色+按钮上。

我的UITableView的代码也在下面。很长很复杂--对不起。只是希望有人能指出为什么会发生这样的事情。谢谢!

普通铅笔

​

inflated pencil

​

代码语言:javascript
复制
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()
}
EN

回答 1

Stack Overflow用户

发布于 2016-02-03 03:08:24

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

你应该检查你的屏幕是纵向的还是横向的,在此基础上你应该为按钮的宽度和高度提供适当的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35157122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档