【问题标题】:Autolayout labels with UIView and Labels inside UIView带有 UIView 的自动布局标签和 UIView 内的标签
【发布时间】:2016-01-07 03:26:02
【问题描述】:

我是自动布局的新手,正在尝试通过 Storyboard 为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(purple) 消失。在Star Rating 之前它可以正常工作。
  • 您的部署目标是什么? iOS 9? iOS 8?
  • iOS 8 及以上,无法使用 StackView。

标签: ios objective-c autolayout


【解决方案1】:

使用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

结果:

【讨论】:

  • 感谢您的努力 rob,我会测试它并告诉您。
  • 你可以向我解释一下如何将星(*)写为字符串,现在我正在使用 IBDesignable 在我的 UIView 中创建星。但是渲染和处理点击会得到很多代码,你的代码看起来很简洁。
  • 您可以从我的答案中复制它并将其粘贴到您的代码中。我通过在 Xcode 的编辑菜单中打开表情符号和符号面板来“输入”它,然后搜索“星号”。
  • 所有在 S/O 的抢夺都是超级明星 :)
猜你喜欢
  • 2015-09-20
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多