我必須在目標 c 中創建一個視圖,上面有兩個標簽,label1 有單行,label2 是基于內容的多行。
根據標簽中的內容,我想調整視圖高度,我該怎么做?
視圖的寬度應該是 20 左右到螢屏寬度,下面的代碼我可以縱向顯示,但在橫向顯示它不正確,它在右側裁剪。我怎樣才能顯示正確的 20 風景?
肖像

風景

UIView *emptyTreeView = [[UIView alloc] initWithFrame:CGRectMake(20,20,self.view.frame.size.width - 40,400)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 30.0, self.view.frame.size.width - 100, 30.0)];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(30.0, 90.0, self.view.frame.size.width - 100, 100.0)];
emptyTreeView.backgroundColor=[UIColor blueColor];
label1.backgroundColor = [UIColor redColor];
label1.textAlignment = NSTextAlignmentCenter;
label1.textColor = [UIColor blackColor];
label1.font = [UIFont boldSystemFontOfSize:18];
label1.numberOfLines = 0;
label1.text = @"The Page Cannot be displayed.";
label2.backgroundColor = [UIColor whiteColor];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor grayColor];
label2.font = [UIFont systemFontOfSize:15];
label2.numberOfLines = 0;
label2.text = @"Please use this feature or contact your internal contact person directly.";
[emptyTreeView addSubview:label1];
[emptyTreeView addSubview:label2];
[self.view addSubview:emptyTreeView];
我做錯了什么嗎?
uj5u.com熱心網友回復:
一些觀察:
不要在
viewDidLayoutSubviews. 這僅用于調整frame值。如果你參考
self.view,參考它bounds,而不是它的frame。前者用于self.view. 后者在其父視圖的坐標系中(現在可能不會引入任何差異,但只是錯誤的坐標系。如果您希望視圖在旋轉時調整大小,請設定它的
autoresizingMask,例如emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;或者
emptyTreeView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;如今,設定幀值和自動調整蒙版大小有點過時了。我們通常會使用約束,例如
- (void)viewDidLoad { [super viewDidLoad]; UIView *emptyTreeView = [[UIView alloc] init]; UILabel *label1 = [[UILabel alloc] init]; UILabel *label2 = [[UILabel alloc] init]; emptyTreeView.backgroundColor = [UIColor blueColor]; label1.backgroundColor = [UIColor redColor]; label1.textAlignment = NSTextAlignmentCenter; label1.textColor = [UIColor blackColor]; label1.font = [UIFont boldSystemFontOfSize:18]; label1.numberOfLines = 0; label1.text = @"The Page Cannot be displayed."; label2.backgroundColor = [UIColor whiteColor]; label2.textAlignment = NSTextAlignmentCenter; label2.textColor = [UIColor grayColor]; label2.font = [UIFont systemFontOfSize:15]; label2.numberOfLines = 0; label2.text = @"Please use this feature or contact your internal contact person directly."; [emptyTreeView addSubview:label1]; [emptyTreeView addSubview:label2]; [self.view addSubview:emptyTreeView]; emptyTreeView.translatesAutoresizingMaskIntoConstraints = false; label1.translatesAutoresizingMaskIntoConstraints = false; label2.translatesAutoresizingMaskIntoConstraints = false; [NSLayoutConstraint activateConstraints:@[ [emptyTreeView.leftAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leftAnchor constant:20], [emptyTreeView.rightAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.rightAnchor constant:-10], [emptyTreeView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor constant:20], [label1.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20], [label1.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20], [label1.topAnchor constraintEqualToAnchor:emptyTreeView.topAnchor constant:20], [label2.leftAnchor constraintEqualToAnchor:emptyTreeView.leftAnchor constant:20], [label2.rightAnchor constraintEqualToAnchor:emptyTreeView.rightAnchor constant:-20], [label2.topAnchor constraintEqualToAnchor:label1.bottomAnchor constant:20], [emptyTreeView.bottomAnchor constraintEqualToAnchor:label2.bottomAnchor constant:20] ]]; }注意
translatesAutoresizingMaskIntoConstraints是false和約束。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/329186.html
下一篇:繼承和訪問屬性
