我对 iOS 和 Objective-C 还是很陌生。
我一直试图让以编程方式生成的 UITableView 正常工作。该表使用自定义 UITableViewCell ,它位于它自己的 xib 文件 MeetingsTableViewCell 中。
.xib 的外观如下:
以及日期字段的约束:
这是运行时的样子:
日期和 session 模式图标/标签在模拟中距离右侧太远了 55 个点(?)。
主视图 Controller 的宽度为 320(我似乎无法更改)。 .xib 的宽度为 375(我似乎也无法更改)。 55分。巧合?我猜不是。
当我使用调试 View 层次结构时,我发现 Cell Wrapper View 是 375 点。我添加了以下代码(可能超出了需要,但我只是在尝试一切以查看是否可以解决):
cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.autoresizesSubviews = true;
cell.cellWrapperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.cellWrapperView.autoresizesSubviews = true;
我还确保我在 MeetingsTableCellView.m 文件中有 layoutIfNeeded 并添加了代码来调整 cellWrapperView
-(void)layoutIfNeeded {
[super layoutIfNeeded];
self.cellWrapperView.frame=self.contentView.bounds;
}
将 View 大小正确调整为 320,但没有移动日期和 session 模式图标/标签。
调试 View 现在如下所示:
TableCellView 和它里面的每个全宽的东西都是 320 磅宽。根据调试 View ,日期和 session 模式图标/标签(正确)为 68 磅宽。 X 位置是 297。我不知道如何让日期和 session 模式图标/标签处于正确位置。
附带说明,如果我将手机转为横向,这些宽度都是 375 而不是 320,但现在不应该是 568 吗?
谢谢!
编辑:创建表的代码。
self.meetings=meetings;
self.meetingsTable=[[UITableView alloc] initWithFrame:self.tableWrapperView.bounds];
self.meetingsTable.delegate = self;
self.meetingsTable.dataSource = self;
self.meetingsTable.autoresizesSubviews = true;
self.meetingsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableWrapperView.autoresizesSubviews = true;
UINib *cellNib=[UINib nibWithNibName"MeetingCellView" bundle:nil];
[self.meetingsTable registerNib:cellNib forCellReuseIdentifier"MeetingsCell"];
[self.meetingsTable reloadData];
[self.tableWrapperView addSubview:self.meetingsTable];
Best Answer-推荐答案 strong>
我认为你有点过于复杂了。
首先,您不需要“cellWrapperView” - contentView 无法做到这一点。
其次,对于单元格布局,您的单元格类中不需要任何代码。
我做了一个快速测试,按照你的方式(大约)布置单元格:
这是结果(我经常使用背景颜色来帮助查看元素框架):
仅使用此代码即可完成...
//
// MeetingsTableViewCell.h
//
#import <UIKit/UIKit.h>
@interface MeetingsTableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *theTitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *theSubTitleLabel;
@property (strong, nonatomic) IBOutlet UILabel *theDateLabel;
@property (strong, nonatomic) IBOutlet UIImageView *theImageView;
@property (strong, nonatomic) IBOutlet UILabel *theModeLabel;
@end
//
// MeetingsTableViewCell.m
//
#import "MeetingsTableViewCell.h"
@implementation MeetingsTableViewCell
@end
//
// TableXIBViewController.h
//
#import <UIKit/UIKit.h>
@interface TableXIBViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
//
// TableXIBViewController.m
//
// Created by Don Mag on 11/14/18.
//
#import "TableXIBViewController.h"
#import "MeetingsTableViewCell.h"
@interface TableXIBViewController ()
@property UITableView *meetingsTable;
@property UIView *tableWrapperView;
@end
@implementation TableXIBViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// inset the table wrapper view by 20-pts so we can see its frame
self.tableWrapperView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 20, 20)];
self.tableWrapperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.tableWrapperView];
self.meetingsTable = [[UITableView alloc] initWithFrame:self.tableWrapperView.bounds];
self.meetingsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.meetingsTable.dataSource = self;
self.meetingsTable.delegate = self;
[self.tableWrapperView addSubview:self.meetingsTable];
UINib *cellNib = [UINib nibWithNibName"MeetingCellView" bundle:nil];
[self.meetingsTable registerNib:cellNib forCellReuseIdentifier"MeetingsCell"];
}
- (nonnull UITableViewCell *)tableViewnonnull UITableView *)tableView cellForRowAtIndexPathnonnull NSIndexPath *)indexPath {
MeetingsTableViewCell *cell = [self.meetingsTable dequeueReusableCellWithIdentifier"MeetingsCell" forIndexPath:indexPath];
cell.theTitleLabel.text = [NSString stringWithFormat"Meeting Title %li", indexPath.row + 1];
cell.theSubTitleLabel.text = [NSString stringWithFormat"Meeting SubTitle %li", indexPath.row + 1];
cell.theDateLabel.text = [NSString stringWithFormat"2018/01/0%li", indexPath.row + 1];
return cell;
}
- (NSInteger)tableViewnonnull UITableView *)tableView numberOfRowsInSectionNSInteger)section {
return 5;
}
@end
而且,为了帮助正确布局单元格 xib,这里是 xib 源代码:
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" rowHeight="97" id="1hH-Nh-Uud" customClass="MeetingsTableViewCell">
<rect key="frame" x="0.0" y="0.0" width="407" height="89"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="1hH-Nh-Uud" id="mnL-wU-yle">
<rect key="frame" x="0.0" y="0.0" width="407" height="88.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="Meeting Title" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6hb-sQ-pY3">
<rect key="frame" x="20" y="11" width="108" height="21"/>
<color key="backgroundColor" red="0.45138680930000002" green="0.99309605359999997" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Meeting Subtitle" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="gfu-sY-NvS">
<rect key="frame" x="20" y="57.5" width="126" height="20.5"/>
<color key="backgroundColor" red="0.45138680930000002" green="0.99309605359999997" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="italicSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="2018/01/01" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fgW-SS-i5C">
<rect key="frame" x="287" y="11" width="100" height="22"/>
<color key="backgroundColor" red="0.45009386540000001" green="0.98132258650000004" blue="0.4743030667" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="gK-nv-uVx"/>
</constraints>
<fontDescription key="fontDescription" type="italicSystem" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="252" text="Meeting Mode" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="FBS-Cm-nZv">
<rect key="frame" x="293.5" y="62" width="87" height="16"/>
<color key="backgroundColor" red="0.45009386540000001" green="0.98132258650000004" blue="0.4743030667" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="clip" translatesAutoresizingMaskIntoConstraints="NO" id="fS2-U6-2dk">
<rect key="frame" x="326.5" y="37" width="21" height="21"/>
<constraints>
<constraint firstAttribute="height" constant="21" id="7U2-rp-N4R"/>
<constraint firstAttribute="width" constant="21" id="cKC-6E-uQn"/>
</constraints>
</imageView>
</subviews>
<color key="backgroundColor" red="0.99953407049999998" green="0.98835557699999999" blue="0.47265523669999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="fS2-U6-2dk" firstAttribute="top" secondItem="fgW-SS-i5C" secondAttribute="bottom" constant="4" id="546-Qt-aMZ"/>
<constraint firstItem="gfu-sY-NvS" firstAttribute="top" relation="greaterThanOrEqual" secondItem="6hb-sQ-pY3" secondAttribute="bottom" constant="4" id="5E7-Wh-Yzw"/>
<constraint firstAttribute="bottomMargin" secondItem="gfu-sY-NvS" secondAttribute="bottom" id="9Bw-TS-gRW"/>
<constraint firstItem="FBS-Cm-nZv" firstAttribute="centerX" secondItem="fgW-SS-i5C" secondAttribute="centerX" id="G9b-b9-Ftn"/>
<constraint firstItem="FBS-Cm-nZv" firstAttribute="top" secondItem="fS2-U6-2dk" secondAttribute="bottom" constant="4" id="GP1-mc-D7y"/>
<constraint firstItem="fS2-U6-2dk" firstAttribute="centerX" secondItem="fgW-SS-i5C" secondAttribute="centerX" id="SLu-6e-2Tb"/>
<constraint firstAttribute="bottomMargin" secondItem="FBS-Cm-nZv" secondAttribute="bottom" id="TmX-2b-f5B"/>
<constraint firstAttribute="trailingMargin" secondItem="fgW-SS-i5C" secondAttribute="trailing" id="V6K-33-cCn"/>
<constraint firstItem="6hb-sQ-pY3" firstAttribute="top" secondItem="mnL-wU-yle" secondAttribute="topMargin" id="VkG-AA-ghx"/>
<constraint firstItem="6hb-sQ-pY3" firstAttribute="leading" secondItem="mnL-wU-yle" secondAttribute="leadingMargin" id="osP-fz-3fL"/>
<constraint firstItem="fgW-SS-i5C" firstAttribute="top" secondItem="mnL-wU-yle" secondAttribute="topMargin" id="paN-Pg-Bzz"/>
<constraint firstItem="gfu-sY-NvS" firstAttribute="leading" secondItem="mnL-wU-yle" secondAttribute="leadingMargin" id="x4d-ek-CE0"/>
</constraints>
</tableViewCellContentView>
<connections>
<outlet property="theDateLabel" destination="fgW-SS-i5C" id="33H-KL-zyk"/>
<outlet property="theImageView" destination="fS2-U6-2dk" id="vgC-kz-BOU"/>
<outlet property="theModeLabel" destination="FBS-Cm-nZv" id="BFI-rF-5c3"/>
<outlet property="theSubTitleLabel" destination="gfu-sY-NvS" id="0bF-xI-U6a"/>
<outlet property="theTitleLabel" destination="6hb-sQ-pY3" id="bRS-vo-UlK"/>
</connections>
<point key="canvasLocation" x="-30.5" y="19.5"/>
</tableViewCell>
</objects>
<resources>
<image name="clip" width="41" height="42"/>
</resources>
</document>
关于ios - super View 更改大小时,UILabel 位置未调整,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/53305319/
|