Ios 如何以编程方式添加标签并以编程方式使用自动布局定位标签
我正在尝试向UIScrollView添加标签。我正在添加顶部约束和前导约束。 标签没有出现 这是我的密码Ios 如何以编程方式添加标签并以编程方式使用自动布局定位标签,ios,uiscrollview,uilabel,autolayout,nslayoutconstraint,Ios,Uiscrollview,Uilabel,Autolayout,Nslayoutconstraint,我正在尝试向UIScrollView添加标签。我正在添加顶部约束和前导约束。 标签没有出现 这是我的密码 self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; self.label.translatesAutoresizingMaskIntoConstraints = NO; self.label.backgroundColor = [UIColor clearColor]; s
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.backgroundColor = [UIColor clearColor];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor=[UIColor whiteColor];
self.label.text = @"Hello";
[self.imageCarouselScrollView addSubview:self.label];
[self bringSubviewToFront:self.label];
self.titleLabelTopConstraint = [NSLayoutConstraint
constraintWithItem:self
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.imageCarouselScrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20];
NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint
constraintWithItem:self
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.imageCarouselScrollView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:20];
[self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]];
您必须在滚动视图的右侧(用于水平滚动)或底部(用于垂直滚动)包含一个约束,以便它可以推断内容大小。我尝试注释您的代码:
self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; //if 'translatesAutoresizingMaskIntoConstraints=NO' frame not work work anymore. Use CGRectZero instead
self.label.translatesAutoresizingMaskIntoConstraints = NO;
self.label.backgroundColor = [UIColor clearColor];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.textColor=[UIColor whiteColor];
self.label.text = @"Hello";
[self.imageCarouselScrollView addSubview:self.label]; //Your self.label added on 'self.imageCarouselScrollView' but your constraints added on self
[self bringSubviewToFront:self.label]; // ??? Why?
self.titleLabelTopConstraint = [NSLayoutConstraint
constraintWithItem:self
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.imageCarouselScrollView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:20]; //it constrain means: "self view top edge equal to self.imageCarouselScrollView bottom + offset 20 "
NSLayoutConstraint *titleLeadingConstraint = [NSLayoutConstraint
constraintWithItem:self
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.imageCarouselScrollView
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:20];//it constrain means: "self view Leading edge equal to self.imageCarouselScrollView Trailing + offset 20 "
[self addConstraints:@[self.titleLabelTopConstraint,titleLeadingConstraint]];
现在我有几个问题:
您是否尝试查看
ui标签
是否在未添加约束的情况下显示?而且,不要试图搞笑。。您的视图背景色没有设置为白色,是吗?对于标签,您只有两个约束来确定其x值和y值,然后其宽度和高度将基于其内容。我还没有真正尝试过使用VisualFormat约束。@user1898829sizeToFits
不使用自动布局,请学习setContentHuggingPriority:forAxis:
和SetContentCompressionResistance:forAxis: