自动布局标签用的UIView和标签内的UIView [英] Autolayout labels with UIView and Labels inside UIView

查看:178
本文介绍了自动布局标签用的UIView和标签内的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的自动版式的东西,试图设计一个指定屏幕宽度紧凑|常规高度通过故事板。它看起来很简单的原因是没有的动态领域。我在下面的Apple文档,并为每个UIView的设置约束。其工作罚款,如果就没有视图(紫色所示)。但是,有一些要求(要隐藏基于星级查看),这就是为什么添加了这个包含的UIView派遣的细节,投诉类别,截图等EG-对于发票号码我设置顶层空间,导致太空,尾部的空格并为标签(以绿色显示)顶层空间,前导空格,尾随空格所以发票号码的身高不会是不明确的代表,所有视图重复相同。 UIView的(紫色)约束尾随空格键:的SuperView,底部的空间:的SuperView,底部的空间:备注等于:15,顶层空间等于:StarRating等于:8 。之后,我加入了紫色视图中的字段约束一样,另外像浏览发票编号。但没有获得对于不同的屏幕大小所需的结果。任何帮助将是非常美联社preciated。

I am new to the autolayout thing, was trying to design the given screen for Compact Width|Regular Height via Storyboard. It seems Very simple cause there is none dynamic fields. I was following apple documentation, and setting constraints for each UIView. Its working fine if there would be no View(shown in purple color). But there is some requirement(have to hide the View based on star rating), thats why added this UIView containing dispatch details, complaint category, screenshot etc. e.g- For Invoice Number I set Top Space,Leading Space, Trailing Space and for the label(shown in green color) Top Space, Leading Space, Trailing Space so that height of invoice Number wouldn't be ambiguous rep, Repeated same for all Views. UIView(purple) constraints are Trailing Space to:SuperView, Bottom space to:SuperView, Bottom space to:Remarks Equals:15, Top Space Equals to:StarRating Equals:8. After that I have added constraints for the fields inside the purple View same as the other Views like Invoice Number. But not getting the desired result for different screen size. Any help would be much appreciated.

推荐答案

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

Use a UITableView to lay out this screen. You can use a static content table view, laid out entirely in the storyboard. Make the purple part its own section. In your UITableViewController subclass, you don't need to implement most data source / delegate methods (because you're using static content). Just override tableView:numberOfRowsInSection: to return 0 for the optional section if you don't want to show it, and use -[UITableView reloadSections:withRowAnimation;] to update the table view. Here's my test code:

#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

结果:

演示

这篇关于自动布局标签用的UIView和标签内的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆