ǰÑÔ
AutoLayoutÓÃÀ´²¼¾ÖÒѾ²»ÊÇʲôÐÂÏÊÊÂÁË£¬ÎÒ֮ǰҲд¹ýÈýƪÈëÃŵÄÎÄÕÂ
1.IB ×Ô¶¯Éú³É
2.IBÍÏ×§
3.´úÂëʵÏÖ
µ±È»£¬Êµ¼Ê¿ª·¢ÖУ¬Èç¹ûÊǶàÈËÐͬ¿ª·¢£¬»ù±¾ÉÏÊÇÆúÓÃStoryboardºÍXIBÎļþµÄ¡£ÒòΪÕâÁ½ÖÖÎļþ±¾ÖÊÉÏÊÇXML£¬
¶àÈËÐͬ¿ª·¢Ò»ÆðÐÞ¸ÄÕâÑùµÄ´óXMLÊÇÒ»¼þºÜµ°ÌÛµÄÊÂÇé¡£
ËùÒÔ£¬´ó²¿·Öʱºò£¬LayoutÔ¼Êø¶¼ÊÇ´¿´úÂëʵÏֵġ£
³ý·ÇijЩ¹«Ë¾ÒªÊµÏÖ×Ô¼ºµÄLayoutÒýÇæ¡£·ñÔò£¬¸ü¶àµÄ»¹ÊÇʹÓõÚÈý·½¿â
4.Masonry
ʹÓÃMasonryÄܹ»ÈÃÔ¼Êø´´½¨±äµÃºÜÈÝÒס£µ«ÊÇ£¬ÎªÁ˸üºÃµÄÀûÓÃAutoLayout£¬ºÜ¶àÔ¼ÊøÏà¹ØµÄ»ù´¡ÖªÊ¶ÒªÁ˽⣬ÕâÑùÄܹ»°ïÖúÄãʵÏÖ¸ü¸´ÔÓ¸üÁé»îµÄ²¼¾Ö¡£
Ô¼ÊøÓÅÏȼ¶
NSLayoutConstraintÓиöÊôÐÔÊÇpriority£¬ÀàÐÍÊÇUILayoutPriority,±¾ÖÊÉÏÆäʵÊÇfloatÀàÐÍ
typealias UILayoutPriority = Float |
¿ÉÒÔȥȡÈçÏÂÖµ£¬µ±È»Ò²¿ÉÒÔÖ±½ÓÉèÖÃfloatÖµ
enum { UILayoutPriorityRequired = 1000, UILayoutPriorityDefaultHigh = 750, UILayoutPriorityDefaultLow = 250, UILayoutPriorityFittingSizeLevel = 50, }; typedef float UILayoutPriority; |
Óô¦£ºµ±Á½¸öÔ¼Êø·¢Éú³åÍ»µÄʱºò£¬»ábreakÓÅÏȼ¶µÍµÄÔ¼Êø
¾Ù¸öÀý×ÓÏÈ£¬
ÀýÈ磬ÎÒÏ£ÍûÓÐÕâÑùµÄÒ»¸öView
1.ˮƽ´¹Ö±¾ÓÖÐ
2.¿í¶È320£¬¸ß¶È200
3.¾àÀë×óÓÒµÄ×îС¼ä¸ô´óÓÚµÈÓÚ20£¨ÎªÁËÊÊÅäСÆÁÄ»£©
Õâʱºò£¬Ìí¼ÓµÄÔ¼ÊøÈçÏÂ

È»ºó£¬ÎÒÃÇ¿´¿´Ô¤ÀÀ

ÔÚ4sÉÏ£¬Ô¤ÀÀÊDz»ÕýÈ·µÄ£¬µ«ÊÇÔÚ´óÆÁÉÏ£¬Ò»ÇÐÕý³£
Èç¹ûÕâʱºòÔËÐÐ4sÄ£ÄâÆ÷£¬»á·¢ÏÖLog
2016-06-03 23:01:40.339 OCTest[1004:26277] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "<NSLayoutConstraint:0x79159c40 H:|-(>=20)-[UIView:0x79159c70] (Names: '|':UIView:0x79158e50 )>", "<NSLayoutConstraint:0x79159dc0 UIView:0x79159c70.centerX == UIView:0x79158e50.centerX>", "<NSLayoutConstraint:0x79159e50 H:[UIView:0x79159c70(320)]>", "<NSLayoutConstraint:0x79144ba0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x79158e50(320)]>" )
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79159e50 H:[UIView:0x79159c70(320)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints
to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging
category on UIView listed in <UIKit/UIView.h>
may also be helpful. |
ÔÒò²»ÄÑ·¢ÏÖ£¬
1.4sÉÏÆÁÄ»¿í¶ÈÊÇ320£¬Èç¹ûÒª±£Ö¤ViewµÄ×óÓÒ¾àÀë±ß¿ò´óÓÚµÈÓÚ20£¬ÄÇôViewµÄ¿í¶ÈСÓÚµÈÓÚ280
2. View»¹ÓиöÔ¼ÊøÊÇ¿í¶ÈΪ320
Õâ¾ÍÊÇÓÅÏȼ¶µÄ×÷ÓÃÁË£¬ÎÒÃÇÉèÖÿí¶ÈµÄÓÅÏȼ¶¸üµÍ£¬ÕâÑùÔÚÓÐÔ¼ÊøºÍ¿í¶È·¢Éú³åÍ»µÄʱºò£¬Ä¬ÈÏbreak¿í¶ÈÔ¼Êø
ÔÚIBÉÏ£¬¿ÉÒÔÕâôÉèÖÃ
ÓÉÓÚĬÈϵÄpriorityÊÇ1000£¬ÕâÀïÎÒÃÇÖ»ÐèÒª½«ÆäÉèÖÃΪ999¾Í¿ÉÒÔÁË

È»ºó£¬Ô¤ÀÀºÍÔËÐкó¶¼Õý³£ÁË

Tips£ºÔÚIBÉÏ£¬Èç¹ûÄã¿´µ½Ò»¸öÔ¼ÊøµÄÊÇÐéÏß±íʾµÄʱºò£¬ÄÇôÕâ¸öÔ¼ÊøµÄÓÅÏȼ¶ÊÇСÓÚĬÈϵÄ
Intrinsic Size£¨ÄÚÔÚ´óС£©
ÎÒÃÇÖªµÀ£¬UILabelºÍUIButtonÔÚÉèÖÃÔ¼ÊøµÄʱºò£¬Ö»ÐèҪȷ¶¨Î»ÖþÍ×ãÒÓÁË£¬¶ø²»ÐèÒªÉèÖôóСµÄÔ¼Êø¡£ÒòΪ£¬UILabelºÍUIButtonÓÐÄÚÔÚ´óС
ÄÚÔÚ´óСÊÇUIViewµÄÒ»¸ö·½·¨£¬Ò²¾ÍÊÇ˵£¬Äã¿ÉÒÔͨ¹ý¼Ì³Ð£¬ÎªUIView¼°Æä×ÓÀà·µ»Ø×Ô¶¨ÒåµÄÄÚÔÚ´óС
-(CGSize)intrinsicContentSize{ return [super intrinsicContentSize]; } |
Ò»¸öºÜµäÐ͵ÄÀý×Ó£¬¼ÙÈçÎÒֻΪһ¸öUIViewÉèÖÃˮƽºÍ´¹Ö±¾ÓÖÐ

Õâʱºò£¬Äã»á·¢ÏÖ£¬Ô¼ÊøÊǺìÉ«µÄ£¬Ò²¾ÍÊÇֻȷ¶¨ÁËλÖã¬Ã»È·¶¨´óС£¬autolayoutÎÞ·¨È·ÈÏviewµÄ״̬
Õâʱºò£¬ÔÚIBÉÏÉèÖÃÄÚÖôóСΪ300*200,ÉèÖÃÄÚÔÚ´óСµÄλÖÃÔÚÏÂͼ×îÏÂÒ»¸öÑ¡Ïî
Intrinsic size

ÉèÖÃÍêÒԺ󣬷¢ÏÖIBÕý³£ÁË¡£
ÕâÑùÉèÖò¢²»»áÓ°ÏìÔËÐÐʱ£¬Ö»»áÈ¥³ýIB¾¯¸æ
ËùÒÔ£¬ÎÒÃÇн¨Ò»¸öUIView×ÓÀ࣬Ȼºó½«¸ÃÊÓͼµÄÀàÐÞ¸Ä
@interface AutoAdjustView : UIView @end @implementation AutoAdjustView -(CGSize)intrinsicContentSize{ return CGSizeMake(300, 200); } @end |
ÔÚÔËÐз¢ÏÖÒ»ÇÐÕý³£¡£
Intrinsic SizeÓÐÁ½¸öºÜ³£¼ûµÄʹÓó¡¾°
³¡¾°Ò»£¬ÎªUILabe£¬UIButtonµÈÉèÖöîÍâµÄPadding
¾Ù¸öÀý×Ó£¬Ò»¸öĬÈϵÄUIButton£¬ÉèÖÃÎÄ×ÖºóµÄЧ¹ûÊÇÕâÑù×ÓµÄ

µ±ÎÒÃÇдһ¸ö×ÓÀà£¬ÖØÔØintrinsicContentSize
@implementation RoundButton
-(CGSize)intrinsicContentSize{
CGSize size = [super intrinsicContentSize];
size.width += size.height;
return size;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.layer.cornerRadius = ceil(self.bounds.size.height/2);
self.layer.masksToBounds = YES;
}
@end |
Ч¹ûÊÇÊÇÕâÑù×ӵģ¬¿ÉÒÔ¿´µ½£¬ÎÒÃDz»¹Üʵ¼Ê¿í¶È¶àÉÙ£¬¶¼Ìí¼ÓÁ˶îÍâµÄpadding¡£
³¡¾°¶þ£¬Èø¸ÊÓͼ¸ù¾Ý×ÓÊÓͼÀ´×ÔÊÊÓ¦×Ô¼ºµÄ´óС(ΪÁË·½±ã£¬ÎÒÔÚIBÉÏÍÏ×§ÁË£¬´úÂëʵÏÖÀàËÆ)
Ê×ÏÈ£¬ÍÏ×§Ò»¸ö±³¾°É«ÎªÀ¶É«µÄview×÷Ϊ¸¸ÊÓͼ£¬È»ºó¸¸ÊÓͼÖÐÌí¼ÓÒ»¸ölabel¡£
¸¸ÊÓͼµÄÉèÖÃ
1.ˮƽ´¹Ö±¾ÓÖÐ
2.Intrinsic Size Ϊ0£¬0
LabelµÄÉèÖÃ
3.¾àÀëtop,bottom,leading,trailing,¾àÀëΪ20

Õâʱºò£¬Ô¤ÀÀÈçÏ£¨½ö½öÐÞ¸ÄlabelÎÄ×Ö£©
 
¿ÉÒÔ¿´µ½£¬ÎÒÃDz¢Ã»ÓÐÉèÖÃÀ¶É«ÊÓͼµÄ¸ß¶È¿í¶È£¬Ëü¿ÉÒÔ×ÔÊÊÓ¦ÄÚ²¿°üº¬ÊÓͼ¡£MBProgressHUDµÄÖмäÇøÓò×ÔÊÊÓ¦´óС¾ÍÊÇÕâô×öµÄ¡£
Hugging Priority
UIViewÓÐÒ»¸ö·½·¨ÊÇ
setContentHuggingPriority: forAxis: |
Sets the priority with which a view resists being made larger than its intrinsic size.
(µ±ViewµÄ´óС´óÓÚÄÚÖôóСµÄʱºò£¬¿¹¾Ü±ä´óµÄÓÅÏȼ¶£¬Ò²¾ÍÊÇ˵£¬ÓÅÏȼ¶Ô½´ó£¬¿¹¾ÜÄÜÁ¦Ô½Ç¿)
ÆäÖУ¬priorityÊÇUILayoutPriority,ºÍÉÏÎĵÄÒ»Ö¡£AxisÊÇ×ø±êϵ
|
typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) { UILayoutConstraintAxisHorizontal = 0,
UILayoutConstraintAxisVertical = 1 }; |
¾ÍÊÇ£¬Äã¿ÉÒÔ·Ö±ðÉèÖÃˮƽ´¹Ö±µÄÓÅÏȼ¶¡£
¾Ù¸öÀý×Ó
IBÍÏ×§ºìÀ¶Á½¸öView
À¶É«ÉèÖÃÈçÏÂ
1.¾àÀë¶¥²¿8
2.¾àÀë×ó±ß20
3.ÄÚÖôóС120£ª50
ºìÉ«ÉèÖÃÈçÏÂ
1.¾àÀë¶¥²¿8
2.¾àÀëÓÒÃæ20
3.ÄÚÖôóС120£ª50 Õâʱºò4.7Ó¢´çÆÁĻԤÀÀÈçÏÂ

½ÓÏÂÀ´£¬Ìí¼ÓÁ½¸öViewµÄˮƽ¼ä¸ôΪ10
¿ÉÒÔ¿´µ½£¬Ô¼Êø·¢Éú³åÍ»ÁË£¬ÒòΪÁ½¸öviewµÄ¿¹ÀÉìÓÅÏȼ¶ÊÇÒ»ÑùµÄ¡£
ÓÉÓÚĬÈϵÄÓÅÏȼ¶ÖµÊÇ250£¬ËùÒÔ£¬ÎÒÃÇÑ¡ÖÐÀ¶É«View£¬ÉèÖÃÓÅÏȼ¶Îª249µÄʱºò£¬»áÀÉìÀ¶É«×Ô¼º£¬Èç¹ûÊÇ251£¬Ôò»áÀÉìºìÉ«

Compression Resistance Priority
Sets the priority with which a view resists being made smaller than its intrinsic size.
Ò²¾ÍÊÇ˵ºÍÉÏÎÄÌáµ½µÄHugging PriorityÀàËÆ£¬Ö»²»¹ýÕâ¸öÊÇ¿¹Ñ¹ËõÓÅÏȼ¶£¬ÕâÀïÔÙ¾ÙÀý×ÓÁË¡£ |
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
|
systemLayoutSizeFittingSize
ºÜ¶àʱºò£¬±ÈÈçUITableviewCell£¬±ÈÈçUICollectionViewCell£¬ÎÒÃÇÐèÒªÖªµÀµ±Ç°ÊÓͼÄÚ²¿×ÓÊÓͼµÄ×îºÏÊʵĵĸ߶ÈÊǶàÉÙ¡£ºÜÐÒÔË£¬UIViewΪÎÒÃÇÌṩÁËÕâ¸ö·½·¨
systemLayoutSizeFittingSize |
Returns the size of the view that satisfies the constraints it holds.
The size of the view that satisfies the constraints it holds.
Ò²¾ÍÊÇ˵£¬¸ù¾Ý×ÓÊÓͼµÄÔ¼Êø£¬°´ÕÕfittings SizeÀ´·µ»Ø×îºÏÊʵĴóС¡£ |
ÆäÖÐfittings Size¿ÉѡֵÈçÏÂ
const CGSize UILayoutFittingCompressedSize;//Âú×ãÔ¼ÊøµÄ×î¿ÉÄÜСµÄ´óС const CGSize UILayoutFittingExpandedSize;//Âú×ãÔ¼ÊøµÄ×î¿ÉÄÜ´óµÄ´óС |
ÒÔTableViewCell¸ß¶È¼ÆËãΪÀý
¼ÙÈçÎÒÃÇÓÐÕâÑùµÄÒ»¸öCell

×ÓÊÓͼ¾ÍÒ»¸öLabel£¬ÉÏÏÂ×óÓÒ¸÷¾ßcontentView¾àÀëΪ8
ÄÇô£¬ÈçºÎÔÚheightForRowAtIndexPathÀï·µ»Ø¸ß¶ÈÄØ£¿
·½·¨Ò»£¬¸ù¾ÝModel¼ÆËã
¸ù¾ÝboundingRectWithSize: options: attributes:
context:À´¼ÆËã¸ß¶È£¬×îºó¼ÓÉÏpadding¡££iOS 7֮ǰ³£Ó㬺ÜÍÁ£¬µ«ÊÇͨÓõķ½·¨¡£
·½·¨¶þ£¬½»¸øUIKit£¬×Ô¼ºÈ¥¼ÆËã¸ß¶È
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewAutomaticDimension; } -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{ return UITableViewAutomaticDimension; } |
·½·¨Èý£¬ÓÃsystemLayoutSizeFittingSizeÀ´¼ÆËã
ÕâÀïÓиöµØ·½Òª×¢Ò⣬ÎÒÃÇÖ»Óй̶¨ÁË¿í¶È£¬²ÅÄܼÆËã³ö¸ß¶È£¬ËùÒÔ£¬´ÓÏÂÎÄ´úÂëÀïÄãÄÜ¿´µ½£¬ÎÒÃÇÌí¼ÓÁËÒ»¸ö¿í¶ÈÔ¼ÊøÐ¡ÓÚµÈÓÚÆÁÄ»¿í¶È
×¢Ò⣺µ±ÓÐaccessoryViewµÄʱºò»òÕßTable²»ÊÇÈ«ÆÁ£¬cellµÄ¿í¶È²¢²»ÊÇÆÁÄ»¿í¶È
@interface TableViewController ()
@property (strong,nonatomic)CustomCell * sizeCell;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:nil] forCellReuseIdentifier:@"cell"];
self.sizeCell = [[NSBundle mainBundle] loadNibNamed:@"CustomCell"
owner:self options:nil].firstObject;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
[self.sizeCell.contentView addConstraint:[NSLayoutConstraint
constraintWithItem:self.sizeCell.contentView attribute:
NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual
toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0
constant:screenWidth]];
}
-(NSArray *)cellTexts{
return @[
@"dagudhau",
@"dagudhau",
@"daguddhaughduahhau",
@"dagudhauhduahgudahughduahguhagudhauhfuadhgudhauhuadh",
@"daghduahgudhaguhdauhguhaguhdughauudhau",
@"agudhauhduahgudahughduahguhagudhauhfuadhgudhauhuadhdaghduahgudhaguhdauhguhaguhdughauudhauagudhauhduahgu
dahughduahguhagudhauhfuadhgudhauhuadhdag",
@"dagudhau",
@"dagudhau",
];
}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
self.sizeCell.customLabel.text = [self cellTexts][indexPath.row];
CGSize fitSize = [self.sizeCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return fitSize.height;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView
*)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self cellTexts].count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"
forIndexPath:indexPath];
cell.customLabel.text = [self cellTexts][indexPath.row];
return cell;
}
|
Ч¹ûÈçÏÂ
iphone 6
iphone 4s
|