[英]UILabel positions not adjusting when superview changes size
我是iOS和Objective-C的新手。
我一直在嘗試以編程方式生成的UITableView
才能正常工作。 該表使用了自定義的UITableViewCell
,它位於其自己的xib文件MeetingsTableViewCell
。
.xib的外觀如下:
以及日期字段的約束:
運行時的外觀如下:
日期和會議模式圖標/標簽在模擬中距離右側太遠了55點(?)。
主視圖控制器的寬度為320(我似乎無法更改該寬度)。 .xib的寬度為375(我似乎也無法更改)。 55分。 巧合? 我猜不是。
使用調試視圖層次結構時,我發現“單元包裝視圖”為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;
}
這樣可以將視圖正確調整為320,但是沒有移動日期和會議模式圖標/標簽。
現在,調試視圖如下所示:
TableCellView及其內部的所有全角物件均為320點寬。 根據調試視圖,日期和會議模式圖標/標簽(正確)寬68點。 X位置是297。我不知道如何將日期和會議模式圖標/標簽放置在正確的位置。
附帶說明一下,如果我將手機放到橫向,那么這些寬度全都是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];
我認為您使事情變得有些復雜。
首先,您不需要“ 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 *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull 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)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)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="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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.