ios – UILabel设置为视图中心

ios – UILabel设置为视图中心,第1张

概述为什么在此代码中绘制的UILabel不在视图中心? //create the view and make it grayUIView *view = [[UIView alloc] init];view.backgroundColor = [UIColor darkGrayColor];//everything for labelUILabel *label = [[UILabel a 为什么在此代码中绘制的UILabel不在视图的中心?

//create the vIEw and make it grayUIVIEw *vIEw = [[UIVIEw alloc] init];vIEw.backgroundcolor = [UIcolor darkGraycolor];//everything for labelUILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,42,21)];//set text of labelNsstring *welcomeMessage = [@"Welcome," stringByAppendingString:@"username"];welcomeMessage = [welcomeMessage stringByAppendingString:@"!"];label.text = welcomeMessage;//set colorlabel.backgroundcolor = [UIcolor darkGraycolor];label.textcolor = [UIcolor whitecolor];//propertIEslabel.textAlignment = NSTextAlignmentCenter;[label sizetoFit];//add the components to the vIEw[vIEw addSubvIEw: label];label.center = vIEw.center;//show the vIEwself.vIEw = vIEw;

line,label.center = vIEw.center;应将标签移动到视图的中心.但是将其移动到标签中心位于视图左侧角落的位置,如下所示.

screenshot http://gyazo.com/2d1c064a671e32c7eb004647232fa430.png

有谁知道为什么?

解决方法 您需要使用框架初始化视图:

UIVIEw *vIEw = [[UIVIEw alloc] initWithFrame:self.vIEw.frame];
总结

以上是内存溢出为你收集整理的ios – UILabel设置为视图中心全部内容,希望文章能够帮你解决ios – UILabel设置为视图中心所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1075803.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存