ios - super View 更改大小时,UILabel 位置未调整
<p><p>我对 iOS 和 Objective-C 还是很陌生。</p>
<p>我一直试图让以编程方式生成的 <code>UITableView</code> 正常工作。该表使用自定义 <code>UITableViewCell</code>,它位于它自己的 xib 文件 <code>MeetingsTableViewCell</code> 中。</p>
<p>.xib 的外观如下:</p>
<p> <a href="/image/pAay4.png" rel="noreferrer noopener nofollow"><img src="/image/pAay4.png" alt=".xib file"/></a> </p>
<p>以及日期字段的约束:</p>
<p> <a href="/image/p1B56.png" rel="noreferrer noopener nofollow"><img src="/image/p1B56.png" alt="date field constraints"/></a> </p>
<p>这是运行时的样子:</p>
<p> <a href="/image/2HGTN.png" rel="noreferrer noopener nofollow"><img src="/image/2HGTN.png" alt="simulation"/></a> </p>
<p>日期和 session 模式图标/标签在模拟中距离右侧太远了 55 个点(?)。</p>
<p>主视图 Controller 的宽度为 320(我似乎无法更改)。 .xib 的宽度为 375(我似乎也无法更改)。 55分。巧合?我猜不是。</p>
<p>当我使用调试 View 层次结构时,我发现 Cell Wrapper View 是 375 点。我添加了以下代码(可能超出了需要,但我只是在尝试一切以查看是否可以解决):</p>
<pre><code>cell.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.autoresizesSubviews = true;
cell.cellWrapperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
cell.cellWrapperView.autoresizesSubviews = true;
</code></pre>
<p>我还确保我在 <code>MeetingsTableCellView.m</code> 文件中有 <code>layoutIfNeeded</code> 并添加了代码来调整 <code>cellWrapperView</code></p>
<pre><code>-(void)layoutIfNeeded {
;
self.cellWrapperView.frame=self.contentView.bounds;
}
</code></pre>
<p>将 View 大小正确调整为 320,但没有移动日期和 session 模式图标/标签。</p>
<p>调试 View 现在如下所示:</p>
<p> <a href="/image/OmeJA.png" rel="noreferrer noopener nofollow"><img src="/image/OmeJA.png" alt="Debug View"/></a> </p>
<p>TableCellView 和它里面的每个全宽的东西都是 320 磅宽。根据调试 View ,日期和 session 模式图标/标签(正确)为 68 磅宽。 X 位置是 297。我不知道如何让日期和 session 模式图标/标签处于正确位置。</p>
<p>附带说明,如果我将手机转为横向,这些宽度都是 375 而不是 320,但现在不应该是 568 吗? </p>
<p>谢谢!</p>
<p>编辑:创建表的代码。</p>
<pre><code>self.meetings=meetings;
self.meetingsTable=[ 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=;
;
;
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为你有点过于复杂了。</p>
<p>首先,您不需要“cellWrapperView” - <code>contentView</code> 无法做到这一点。</p>
<p>其次,对于单元格布局,您的单元格类中不需要<em>任何</em>代码。</p>
<p>我做了一个快速测试,按照你的方式(大约)布置单元格:</p>
<p> <a href="/image/ZQmBL.png" rel="noreferrer noopener nofollow"><img src="/image/ZQmBL.png" alt="enter image description here"/></a> </p>
<p>这是结果(我经常使用背景颜色来帮助查看元素框架):</p>
<p> <a href="/image/0hJwN.png" rel="noreferrer noopener nofollow"><img src="/image/0hJwN.png" alt="enter image description here"/></a>
<a href="/image/AakV7.png" rel="noreferrer noopener nofollow"><img src="/image/AakV7.png" alt="enter image description here"/></a> </p>
<p>仅使用此代码即可完成...</p>
<pre><code>//
//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
</code></pre>
<hr/>
<pre><code>//
//MeetingsTableViewCell.m
//
#import "MeetingsTableViewCell.h"
@implementation MeetingsTableViewCell
@end
</code></pre>
<hr/>
<pre><code>//
//TableXIBViewController.h
//
#import <UIKit/UIKit.h>
@interface TableXIBViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
</code></pre>
<hr/>
<pre><code>//
//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 {
;
// Do any additional setup after loading the view.
self.view.backgroundColor = ;
// inset the table wrapper view by 20-pts so we can see its frame
self.tableWrapperView = [ initWithFrame:CGRectInset(self.view.frame, 20, 20)];
self.tableWrapperView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
;
self.meetingsTable = [ initWithFrame:self.tableWrapperView.bounds];
self.meetingsTable.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.meetingsTable.dataSource = self;
self.meetingsTable.delegate = self;
;
UINib *cellNib = ;
;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
MeetingsTableViewCell *cell = ;
cell.theTitleLabel.text = ;
cell.theSubTitleLabel.text = ;
cell.theDateLabel.text = ;
return cell;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return5;
}
@end
</code></pre>
<hr/>
<p>而且,为了帮助正确布局单元格 xib,这里是 xib 源代码:</p>
<pre><code><?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="PgK-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>
</code></pre></p>
<p style="font-size: 20px;">关于ios -superView 更改大小时,UILabel 位置未调整,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/53305319/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/53305319/
</a>
</p>
页:
[1]