vishnu
vishnu

Reputation: 233

how to give separate height and width constraint for specified screen size for eg giving different constraints for iphone and ipad?

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.

Upvotes: 1

Views: 767

Answers (2)

Syed Qamar Abbas
Syed Qamar Abbas

Reputation: 3677

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

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

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

Upvotes: 3

Related Questions