在UIView中有UIView和Labels的Autolayout标签

我是新来的自动布局的东西,试图通过故事板devise给定屏幕的Compact Width|Regular Height 。 这似乎很简单,因为没有dynamic的领域。 我正在关注苹果文档,并为每个UIView设置约束。 它的工作正常,如果没有View(shown in purple color) 。 但有一些要求(必须隐藏视图基于星级评分),这就是为什么添加这个UIView包含调度细节,投诉类别,截图等。例如,对于Invoice Number I set Top Space,Leading Space, Trailing Spacelabel(shown in green color) Top Space, Leading Space, Trailing Space以便发票号码的高度不会含糊不清,重复相同的所有视图。 UIView(紫色)约束是Trailing Space to:SuperView, Bottom space to:SuperView, Bottom space to:Remarks Equals:15, Top Space Equals to:StarRating Equals:8 。 之后,我添加了紫色视图中的字段与其他视图相同的约束,如Invoice Number 。 但是对于不同的屏幕尺寸没有得到期望的结果。 任何帮助将非常感激。

使用UITableView来布局这个屏幕。 您可以使用静态内容表格视图,完全放在故事板中。 使紫色部分自己的部分。 在你的UITableViewController子类中,你不需要实现大多数数据源/委托方法(因为你使用的是静态内容)。 只要重写tableView:numberOfRowsInSection:如果你不想显示它,返回0作为可选部分,并使用-[UITableView reloadSections:withRowAnimation;]更新表视图。 这是我的testing代码:

 #import "ViewController.h" @interface ViewController () @property (strong, nonatomic) IBOutlet UITableViewCell *topMarginCell; @property (strong, nonatomic) IBOutlet UIButton *star1; @property (strong, nonatomic) IBOutlet UIButton *star2; @property (strong, nonatomic) IBOutlet UIButton *star3; @property (strong, nonatomic) IBOutlet UIButton *star4; @property (strong, nonatomic) IBOutlet UIButton *star5; @property (strong, nonatomic) NSArray<UIButton *> *starButtons; @property (nonatomic) NSInteger rating; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.starButtons = @[ self.star1, self.star2, self.star3, self.star4, self.star5 ]; self.tableView.backgroundColor = self.topMarginCell.backgroundColor; } - (void)reloadDispatchSection { [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone]; } - (IBAction)starButtonWasTapped:(UIButton *)button { NSInteger buttonIndex = [self.starButtons indexOfObject:button]; if (buttonIndex == NSNotFound) { return; } self.rating = buttonIndex + 1; [self.starButtons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj setTitle:(idx <= buttonIndex ? @"★" : @"☆") forState:UIControlStateNormal]; }]; [self reloadDispatchSection]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 1 && (self.rating == 0 || self.rating >= 4)) { return 0; } else { return [super tableView:tableView numberOfRowsInSection:section]; } } @end 

结果:

演示