0
let cell = tableview.dequeueReusableCell(withIdentifier: "cell1") as! viewTableViewCell
let head = titlelist[indexPath.row] as! String
cell.lbl.text = head.uppercased()    
if(UIScreen.main.bounds.size.height == 1366)
{
    cell.img.heightAnchor.constraint(equalToConstant: 216).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 395).isActive = true
    self.view.updateConstraints()
}

I want to change the width and height constraint for my imageview img in cell.but its not updating.any help is appreciated, thanks.

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
vishnu
  • 213
  • 1
  • 13

2 Answers2

2

You can try

cell.img.translatesAutoresizingMaskIntoConstraints = false

if UIDevice.current.userInterfaceIdiom == .pad {
    //iPad
    cell.img.heightAnchor.constraint(equalToConstant: 400).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 500).isActive = true
} 
else if UIDevice.current.userInterfaceIdiom == .phone {
    //iPhone
    cell.img.heightAnchor.constraint(equalToConstant: 216).isActive = true
    cell.img.widthAnchor.constraint(equalToConstant: 395).isActive = true
} 

cell.img.topAnchor.constraint(equalTo: cell.contentView.topAnchor, constant: 5).isActive = true
cell.img.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: 5).isActive = true
cell.img.centerXAnchor.constraint(equalTo: cell.contentView.centerXAnchor, constant: 5).isActive = true

this requires dynamic table height

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Just like in below Image. Select your constraint lets say 'Width' and after select look at the left opened panel you'll see a + button before constant just tap on it a pop up will open with a menu like

  • Width: Compact
  • Height: Regular
  • Width: Any

This is for iPhone. To create another constant for iPad just change width form Compact to Regular

enter image description hereenter image description hereenter image description here

Now your constraint has two constant variables. One for iPhones and one for iPads. Assign different constant values as per your requirement.

PS: You should not change Height and Width from + is different thing. Compact Width and Regular Height means an iPhone and Regular Width and Regular Height means an iPad

Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
  • do you have soucre file where i can go deeper abouth the subject – vishnu Feb 27 '18 at 10:34
  • Sure. Download it from here https://drive.google.com/file/d/1VQkvG_hWNfvKaCriYn0qGToGWks3-nSg/view?usp=sharing – Syed Qamar Abbas Feb 27 '18 at 10:39
  • Open the storyboard. I have a cell with height 300 and imageView with height & width = 100 for iPhones and 250 for iPads. You can check it using preview feature of storyboard instead of directly running the app. – Syed Qamar Abbas Feb 27 '18 at 10:40
  • i was referring to documentation regarding this topic.i have sucessfully changed my width and height constants – vishnu Feb 27 '18 at 11:13
  • `Adaptive UI` => https://developer.apple.com/design/adaptivity/ `UITraitCollection` => Doc https://developer.apple.com/documentation/uikit/uitraitcollection – Syed Qamar Abbas Feb 27 '18 at 11:16