Can I use setFrame and autorun in the same view?

I want to add a padding to all of my buttons, so I subclassed UIButton, and among other changes, I wanted to add a padded padding using the setFrame method. Everything worked except setFrame. I checked and I found out that if I uncheck "using AutoLayout" in this view, I can use setFrame and it works. Is there any way around this? I really want to use autolayout because it helps make the application enjoyable on iphone 5 and earlier devices. But I would also like to use setFrame in my subclass to make my life easier.

Summing up, I asked the question: can I use autostart, as well as programmatically configure the UIView frame?

+61
iphone cocoa-touch autolayout interface-builder frame
Nov 01 '12 at 23:10
source share
5 answers

Yes, it can be done.

If you set the translatesAutoresizingMaskIntoConstraints = YES setFrame: calls will automatically translate at run time into layout constraints based on the current autoresizingMask . This allows you to mix the frame layout with the constraint based layout.

For example, you can use Auto Layout to determine the layout of all subzones of a view, but still call setFrame: to set the size and position of the view itself. From your point of view, you are making a layout with a combination of automatic layout and direct frame processing. But the system actually uses restrictions to handle everything.

However, there is one big caveat about using translatesAutoresizingMaskIntoConstraints .

When you do this, you still need to make sure that these automatic constraints can be satisfied by the rest of your constraints.

So, suppose that there are already limitations that determine the size and position of your view, and then you also set translatesAutoresizingMaskIntoConstraints = YES and call setFrame: Calling setFrame: will create new constraints on the view, which are likely to conflict with existing constraints.

(Actually, this error happens often. If you ever see a log message complaining about conflicting restrictions, and one of these restrictions is NSAutoresizingMaskLayoutConstraint , then what you see is an automatic limit conflict. This is a simple error, because translatesAutoresizingMaskIntoConstraints = YES is the default, so if you are setting restrictions on the code, you need to forget to disable it if you do not need these automatic restrictions.)

In contrast, suppose that there are already limitations that determine the size and position of your view, but then you set translatesAutoresizingMaskIntoConstraints = NO before calling setFrame: In this case, your calls to setFrame: will not create new constraints, so there will be no conflict between the individual constraints. However, in this case, there is still a β€œconflict” between the restrictions and the set frame value. The next time the automatic layout is called, it will see the existing limitations in the view, calculate the required frame value and set the frame to the desired value, resetting the value that you set manually.

For more information, see "Accept Auto Layout" in the Apple Cocoa Auto Layout Guide .

+196
Nov 24 '12 at 9:01
source share

What turned out to be the easiest for me was to remove the view that I wanted to move / resize from its supervisor, set its frame , and then add it back. For example, take a custom UILabel in a subclass of UITableViewCell :

 [cell.myLabel removeFromSuperview]; cell.myLabel.frame = someFrameIGenerated; [cell.contentView addSubview:cell.myLabel]; 
+16
Oct 30 '13 at 5:19
source share

Something that worked for me simply added an IBOutlet for my constraints to the view controller, and then changed the constant property on the output.

For example, to change the x-start of a view, set the output from this view, the leading constraint, to your controller. In other words, create the @IBOutlet var labelXOrigin: NSLayoutConstraint! . Then, when you want to adjust the x position, just do something like

 self.labelXOrigin.constant = 20.0 // Or whatever origin you want to set. 
+2
Mar 20 '16 at 21:59
source share

The best way to set the subtask frame is with the viewWillLayoutSubviews method in autodetection projects.

set the property translautAutoresizingMaskIntoConstraints = true this will work after the view or button on the button for installation is displayed, immediately after the view.

+2
Jun 14 '16 at 7:00
source share

If your application does not support multiple device orientations.

In viewDidLoad set the frame of the view you want to resize

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [aView setFrame:CGRectMake(15, 15, 100, 100)]; } 

Then in viewDidLayoutSubviews :

 -(void)viewDidLayoutSubviews { [self.view setTranslatesAutoresizingMaskIntoConstraints:NO]; } 

I'm not sure, but I think that if you use this solution, switching from one orientation to another can cause problems.

0
Sep 11 '15 at 11:10
source share



All Articles