UIView内的UIView和标签Autolayout标签

问题描述:

我是新来的自动布局的东西,试图通过故事板设计给定屏幕Compact Width|Regular Height。这似乎很简单,因为没有动态的领域。我正在关注苹果文档,并为每个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。但不能获得不同屏幕尺寸的理想效果。任何帮助将非常感激。UIView内的UIView和标签Autolayout标签

+0

什么结果你好吗,和你有什么期待? –

+0

期望与屏幕截图中显示的相同,在添加所有约束条件后,UIView(紫色)消失。它工作正常,直到“星级”。 –

+0

你的部署目标是什么? iOS 9? iOS 8? –

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

#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 

结果:

demo

+0

感谢您的努力抢夺,我会测试它,让你知道。 –

+0

还有一件事你可以向我解释如何将这个星号(*)写成字符串,现在我正在使用IBDesignable在我的UIView中创建这个星号。但渲染和处理点击它会得到很多代码,你的代码看起来很简单,很干净。 –

+0

您可以从我的答案中复制它并将其粘贴到您的代码中。我通过从Xcode的编辑菜单中打开Emoji&Symbols面板,然后搜索“star”来“键入”它。 –