2

So I have a static tableview in place and I'd like to change the position of the label inside the first tableviewcell. I have IBOutlet for the first cell.

This is how I try to change the label's position:

UILabel *label1 = [tempTC.cell1.contentView.subviews firstObject];
label1.frame = CGRectMake(10, 10, 10, 10);

The problem is that the frame doesn't change, however I can do everything else to the label like change it's text and size etc, but changing the frame doesn't seem to work.

Is there something I am missing?

Samuli Lehtonen
  • 3,840
  • 5
  • 39
  • 49

4 Answers4

1

When using auto layout, you should set frames, you should change the constraints instead. The easiest way to do this is to make IBOutlets to the ones you need to change, and change the constant value of the constraint. For instance, if you had a constraint to the left side called leftCon, you could do something like this:

self.leftCon.constant = 30;
rdelmar
  • 103,982
  • 12
  • 207
  • 218
1

When you want to change frame subview those were constraint in XIB. You need to set translatesAutoresizingMaskIntoConstraints = YES.

label1.translatesAutoresizingMaskIntoConstraints = YES;
label1.frame = CGRectMake(10, 10, 10, 10);

For detail: Can I use setFrame and autolayout on the same view?

Community
  • 1
  • 1
Linh Nguyen
  • 2,006
  • 1
  • 20
  • 16
0

First of all according to comments, disable auto layout. Second if You want refer to textLabel use [[UITableViewCell textLabel] setFrame:CGRectMake(10,10,10,10)];

BTW. If You'r first UITablViewCell is special I suggest use .xib and subclass UITableviewCell. Apply that subclass to prototype UITableViewCell in storyboard, then add this cell protype cell as property/global variable.

If you need more description please ask :).

Błażej
  • 3,617
  • 7
  • 35
  • 62
0

Make a custom UITableViewCell.

Look at the example below. Inside tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) method I am dynamically changing TimeStatus (UIView) frame size:

.
.
.
let cell:OneTVChannelTableViewCell = tableView.dequeueReusableCellWithIdentifier(OneCurrentChannelTCIdentifier, forIndexPath: indexPath) as OneTVChannelTableViewCell
.
.
.
cell.setForRepairDynamicViewWidth(channelTimeStatusWidth)
.
.
.

And here is my simple custom UITableViewCell:

class OneTVChannelTableViewCell: UITableViewCell {

@IBOutlet weak var Number: UILabel!
@IBOutlet weak var Image: UIImageView!
@IBOutlet weak var Title: UILabel!
@IBOutlet weak var TimeStatus: UIView!
@IBOutlet weak var TimeBack: UIView!

var RepairWidth:CGFloat = 0.0

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override func layoutSubviews() {
    super.layoutSubviews()

    TimeStatus.frame.size.width = RepairWidth
}

func setForRepairDynamicViewWidth(width:CGFloat)
{
    RepairWidth = width
}  
}
sabiland
  • 2,526
  • 1
  • 25
  • 24