简体   繁体   English

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

[英]Why does my UIButton affect my UIImageView in Swift?

I'm building a simple app in Swift, and the app contains a png image, a UISegmentedControl, and a UIButton. 我正在Swift中构建一个简单的应用程序,该应用程序包含一个png图像,一个UISegmentedControl和一个UIButton。 Selecting different controls on the UISegmentedControl resizes an image of a pizza. 在UISegmentedControl上选择不同的控件将调整披萨图像的大小。 When I click on "Large" in the UISegmentedControl, and then I press a button on the interface called submitOrderDisplay, it shrinks the image from its large size to its small size. 当我单击UISegmentedControl中的“大”,然后在界面上按下一个名为SubmitOrderDisplay的按钮时,它将图像从大尺寸缩小到小尺寸。 I know it has something to do with the setTitle method I am using at the very bottom of the code, but I don't understand why that causes the image to reset to its default small size. 我知道它与我在代码的最底部使用的setTitle方法有关,但是我不明白为什么这会导致图像重置为其默认的小尺寸。 If I remove the setTitle method, the resizing does not occur after I click the UIButton. 如果删除setTitle方法,则在单击UIButton后不会发生大小调整。 I don't want the image to change size after the button is pressed. 按下按钮后,我不希望图像更改大小。 How do I achieve that? 我该如何实现?

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)
}
}

Write the configuration of the button inside the viewDidLoad function: 在viewDidLoad函数中编写按钮的配置:

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

I understand that you want to put the title Update Order. 我们了解到您想放置标题“更新订单”。

If you want to change the title when pressed, you must call it for another State of the button like: 如果要在按下时更改标题,则必须为按钮的另一个状态调用它,例如:

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

If you change the configuration of the button for different states when an event happens, that must be why the size is ressetting. 如果在事件发生时将按钮的配置更改为不同的状态,则这一定是重置大小的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM