ios - 带有 UIView 的自动布局标签和 UIView 内的标签

标签 ios objective-c autolayout

我是自动布局方面的新手,正在尝试通过 Storyboard 为 Compact Width|Regular Height 设计给定的屏幕。看起来很简单,因为没有动态字段。我正在关注苹果文档,并为每个 UIView 设置约束。如果没有 View(以紫色显示),它工作正常。但是有一些要求(必须隐藏基于星级的 View ),这就是为什么添加这个包含调度详细信息、投诉类别、屏幕截图等的 UIView。例如-对于 Invoice Number 我设置了 Top Space、Leading Space、Trailing Space 标签(以绿色显示)顶部空间、前导空间、尾随空间,这样发票编号的高度就不会出现歧义,对所有 View 重复相同。 UIView(purple) 约束是 Trailing Space to:SuperView, Bottom space to:SuperView, Bottom space to:Remarks Equals:15, Top Space Equals to:StarRating Equals:8。之后,我为紫色 View 中的字段添加了与其他 View (如 Invoice Number)相同的约束。但是对于不同的屏幕尺寸没有得到想要的结果。任何帮助将非常感激。

最佳答案

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

#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

关于ios - 带有 UIView 的自动布局标签和 UIView 内的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34633425/

相关文章:

css - 为什么 iOS Chrome v. iOS Safari 上的字体大小不同?

iphone - IB - 无法为 View Controller 分配类

ios - 如何使用约束设置自身 View 高度?

ios - 如何调整调整 UICollectionView 大小的 Autolayout 位置?

ios - xcodebuild 命令行忽略 GCC_PREPROCESSOR_DEFINITIONS

ios - WatchKit:捆绑标识符和 WKAppBundleIdentifer 之间有什么区别?为什么它会导致应用程序组无法工作?

iphone - 未调用 UIView layoutSubviews

php - 雅虎财经数据查询计数和效率

ios - 调整 UICollectionView 大小

ios - tableView 有时在两行中显示内容