Y
Y
Yura Imashev2014-12-07 09:25:43
iOS
Yura Imashev, 2014-12-07 09:25:43

Swift - how to move and resize UIImageView programmatically?

In the View Controller Scene, I have created a UIView on which lies a UIImageView containing an image.
Depending on the screen size of the device, on startup, this ImageView should resize.
(to be precise, it should be square, with a side equal to the width of the screen)
I created a child class MyImageView in which I changed the constructor, adding code that should change the size and position of the object, but it does not change anything at all.

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
        
    var screenWidth = UIScreen.mainScreen().bounds.width
        
    self.bounds = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenWidth, height: screenWidth))
    self.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: screenWidth, height: screenWidth))
}

I was told that I misunderstood the logic of UIKit. What I do in the code will overwrite what is set in the View Controller Scene. But how then to change the object programmatically?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Flie, 2014-12-07
@Flie

And you instantiate MyImageView with what method?

Y
Yura Imashev, 2014-12-07
@YuraImashev

In the View Controller Scene, I add an Image View and in the Identity Inspector, in the Custom Class section, I indicate that this is an object of the MyImageView class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question