ios - 为什么我的 UIButton 会影响 Swift 中的 UIImageView?

标签 ios swift uiimageview uibutton

我正在 Swift 中构建一个简单的应用程序,该应用程序包含一个 png 图像、一个 UISegmentedControl 和一个 UIButton。在 UISegmentedControl 上选择不同的控件可调整披萨图像的大小。当我单击 UISegmentedControl 中的“大”,然后按下界面上名为“submitOrderDisplay”的按钮时,它将图像从大尺寸缩小到小尺寸。我知道这与我在代码最底部使用的 setTitle 方法有关,但我不明白为什么这会导致图像重置为其默认的小尺寸。如果我删除 setTitle 方法,则单击 UIButton 后不会发生调整大小。我不希望按下按钮后图像改变大小。我如何实现这一目标?

import UIKit

class SecondViewController: UIViewController{


@IBOutlet weak var sizeChoiceControl: UISegmentedControl!
@IBOutlet weak var submitOrderDisplay: UIButton!
@IBOutlet weak var myImageView: UIImageView!


@IBAction func sizeSelected() {

    if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Small"){
        myImageView.setHeight(height: 85)
        myImageView.setWidth(width: 85)
        myImageView.setX(x: 146)
        myImageView.setY(y: 63)
    }
    else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Medium"){
        myImageView.setHeight(height: 100)
        myImageView.setWidth(width: 100)
        myImageView.setX(x: 139)
        myImageView.setY(y: 55)
    }
    else if(sizeChoiceControl.titleForSegmentAtIndex(sizeChoiceControl.selectedSegmentIndex)! == "Large"){
        myImageView.setHeight(height: 115)
        myImageView.setWidth(width: 115)
        myImageView.setX(x: 131)
        myImageView.setY(y: 48)
    }
}

@IBAction func SubmitOrderClicked(sender: UIButton) { 

    submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)
}
}

最佳答案

在viewDidLoad函数中写入按钮的配置:

submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Normal)

我知道您想将标题设为“更新订单”。

如果您想在按下时更改标题,则必须为按钮的另一种状态调用它,例如:

submitOrderDisplay.setTitle("Update Order", forState: UIControlState.Highlihgted)

如果在事件发生时更改按钮的不同状态的配置,这一定是大小重新设置的原因。

关于ios - 为什么我的 UIButton 会影响 Swift 中的 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43062745/

相关文章:

ios - watchOS2 CMDeviceMotion 不起作用

ios - 无法识别Facebook App邀请“FBSDKAppInviteContent”

ios - 导航 Controller (按下按钮 "back"时如何释放内存)

arrays - Swift - 基于比较两个数组值的结果的新数组

ios - 如何获取这段 Swift 代码以接收 PHP 脚本的返回值?

ios - 创建一个由图标制成的按钮,以便图标填充它

iphone - 使用 ASIHTTPRequest 的 UITableViewCell 中的 UIImageView 性能

iphone - 影响 UIImageView 的 Z 顺序

ios - swift 2.0 中的关闭声明

ios - 只允许一个用户点击推送通知