Can't get a view to change it's size
up vote
1
down vote
favorite
I have a UIImageView
inside a ForecastCell Class
(for a UICollectionView
), anyhow, I can't get the UIImageView
inside the cell to change it's size. This is what I tried:
private func setupWeatherIcon()
self.addSubview(mWeatherIcon)
//mWeatherIcon.frame.size.width = CGFloat(self.frame.width) / 2
//mWeatherIcon.frame.size.height = CGFloat(self.frame.height) / 2
mWeatherIcon.frame.size.width = 20
mWeatherIcon.frame.size.height = 20
mWeatherIcon.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor).isActive = true
mWeatherIcon.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
This is mWeatherIcon:
var mWeatherIcon: UIImageView =
let image = UIImageView(image: UIImage(named: "partly-cloudy"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
()
No matter what width and height I set, It always stays the same width and height.
ios swift uicollectionview
add a comment |
up vote
1
down vote
favorite
I have a UIImageView
inside a ForecastCell Class
(for a UICollectionView
), anyhow, I can't get the UIImageView
inside the cell to change it's size. This is what I tried:
private func setupWeatherIcon()
self.addSubview(mWeatherIcon)
//mWeatherIcon.frame.size.width = CGFloat(self.frame.width) / 2
//mWeatherIcon.frame.size.height = CGFloat(self.frame.height) / 2
mWeatherIcon.frame.size.width = 20
mWeatherIcon.frame.size.height = 20
mWeatherIcon.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor).isActive = true
mWeatherIcon.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
This is mWeatherIcon:
var mWeatherIcon: UIImageView =
let image = UIImageView(image: UIImage(named: "partly-cloudy"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
()
No matter what width and height I set, It always stays the same width and height.
ios swift uicollectionview
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a UIImageView
inside a ForecastCell Class
(for a UICollectionView
), anyhow, I can't get the UIImageView
inside the cell to change it's size. This is what I tried:
private func setupWeatherIcon()
self.addSubview(mWeatherIcon)
//mWeatherIcon.frame.size.width = CGFloat(self.frame.width) / 2
//mWeatherIcon.frame.size.height = CGFloat(self.frame.height) / 2
mWeatherIcon.frame.size.width = 20
mWeatherIcon.frame.size.height = 20
mWeatherIcon.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor).isActive = true
mWeatherIcon.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
This is mWeatherIcon:
var mWeatherIcon: UIImageView =
let image = UIImageView(image: UIImage(named: "partly-cloudy"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
()
No matter what width and height I set, It always stays the same width and height.
ios swift uicollectionview
I have a UIImageView
inside a ForecastCell Class
(for a UICollectionView
), anyhow, I can't get the UIImageView
inside the cell to change it's size. This is what I tried:
private func setupWeatherIcon()
self.addSubview(mWeatherIcon)
//mWeatherIcon.frame.size.width = CGFloat(self.frame.width) / 2
//mWeatherIcon.frame.size.height = CGFloat(self.frame.height) / 2
mWeatherIcon.frame.size.width = 20
mWeatherIcon.frame.size.height = 20
mWeatherIcon.centerXAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerXAnchor).isActive = true
mWeatherIcon.centerYAnchor.constraint(equalTo: self.safeAreaLayoutGuide.centerYAnchor).isActive = true
This is mWeatherIcon:
var mWeatherIcon: UIImageView =
let image = UIImageView(image: UIImage(named: "partly-cloudy"))
image.translatesAutoresizingMaskIntoConstraints = false
return image
()
No matter what width and height I set, It always stays the same width and height.
ios swift uicollectionview
ios swift uicollectionview
asked Nov 10 at 15:52
John Doah
149417
149417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You need width and height constraints
self.contentView.addSubview(mWeatherIcon)
NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])
Don't forget to implement this method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You need width and height constraints
self.contentView.addSubview(mWeatherIcon)
NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])
Don't forget to implement this method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
add a comment |
up vote
0
down vote
You need width and height constraints
self.contentView.addSubview(mWeatherIcon)
NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])
Don't forget to implement this method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
add a comment |
up vote
0
down vote
up vote
0
down vote
You need width and height constraints
self.contentView.addSubview(mWeatherIcon)
NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])
Don't forget to implement this method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
You need width and height constraints
self.contentView.addSubview(mWeatherIcon)
NSLayoutConstraint.activate([
mWeatherIcon.centerXAnchor.constraint(equalTo:contentView.centerXAnchor),
mWeatherIcon.centerYAnchor.constraint(equalTo:contentView.centerYAnchor),
mWeatherIcon.widthAnchor.constraint(equalTo:contentView.widthAnchor,multiplier:0.5),
mWeatherIcon.heightAnchor.constraint(equalTo:contentView.heightAnchor,multiplier:0.5)
])
Don't forget to implement this method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
edited Nov 10 at 16:57
answered Nov 10 at 16:15
Sh_Khan
33.8k41124
33.8k41124
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
add a comment |
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
I don't want it to be really 20 20, just an example. I tried giving it self.widthanchor and divide it by 2 but it didn't even build, I tried casting but still nothing. How can I get the self.widthanchor to be the width of the image?
– John Doah
Nov 10 at 16:51
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
@JohnDoah see edit...........
– Sh_Khan
Nov 10 at 16:58
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240644%2fcant-get-a-view-to-change-its-size%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown