在我的快速代码中,我使用自动布局约束。有些是直接从情节提要添加的,有些是以编程方式添加到 UIScrollView 上的。
当应用程序以阿拉伯语版本(RTL 方向)运行时,从故事板添加的自动布局约束会自动切换到 RTL 方向,而无需我做任何努力,但以编程方式添加到 UIScrollView 上的约束不会被翻转。
这是我在 UISCrollView 上添加约束的示例:
var previousView: UIView = self.productsScrollView
for productObj in productsArray {
let productViewObj: ProductView = ProductView(frame: CGRectMake(0, 0, self.productsScrollView.frame.size.width, self.productsScrollView.frame.size.height), productObj: productObj)
productViewObj.translatesAutoresizingMaskIntoConstraints = false
self.productsScrollView.addSubview(productViewObj)
// Left Constraint
let leftConstraint = NSLayoutConstraint(item: productViewObj,
attribute: .Left,
relatedBy: .Equal,
toItem: previousView,
attribute: (previousView === self.productsScrollView) ? .Left : .Right,
multiplier: 1.0,
constant: 0.0);
self.productsScrollView .addConstraint(leftConstraint)
// Top Constraint
let topConstraint = NSLayoutConstraint(item: productViewObj,
attribute: .Top,
relatedBy: .Equal,
toItem: self.productsScrollView,
attribute: …Run Code Online (Sandbox Code Playgroud) 我在 UITableViewCell 中有一个标签,我希望 TableViewCell 的高度自动根据标签高度。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TWTTweetTableViewCell *cell = (TWTTweetTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"TWTTweetTableViewCell" forIndexPath:indexPath];
TWTTweet *tweet = self.tweets[indexPath.row];
cell.tweetMessage.text = tweet.tweetMessage;
cell.timestamp.text = [tweet howLongAgo];
cell.tag = indexPath.row;
TWTUser *user = [[TWTTwitterAPI sharedInstance] userForId:tweet.userId];
cell.user.text = user.username;
return cell;
Run Code Online (Sandbox Code Playgroud)
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 165;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Tapped a tweet
}
Run Code Online (Sandbox Code Playgroud)
我有一个表格视图,其中有tableViewHeader一个充满单元格的部分。
在创建 之前UITableView,我创建了一个名为 的视图topSection,该视图具有视图的宽度和基于从 API 返回的文本的可变高度。
当我加载视图控制器时,由于 API 调用正在进行中,我可以非常简短地看到topSection(即UITableView.tableViewHeader)位于UITableViewCells. 然后,当reloadData在 my 中调用didSet来渲染表视图时,一切都会正确布局。
topSection在将其添加到之前,我不会以任何方式对其进行限制UITableView
我也尝试将 the 限制top为topSectionthetableView top但那也不起作用。
let listingsTableView = UITableView()
listingsTableView.backgroundColor = .white
listingsTableView.dataSource = self
listingsTableView.delegate = self
listingsTableView.rowHeight = 78
listingsTableView.bounces = false
listingsTableView.allowsSelection = false
listingsTableView.tableHeaderView = topSection
listingsTableView.register(ListingsHeaderView.self, forHeaderFooterViewReuseIdentifier: "HeaderView")
listingsTableView.register(ListingsTableViewCell.self, forCellReuseIdentifier: "ListingsCell")
view.addSubview(listingsTableView)
constrain(listingsTableView, topSection) { tableview, topSection in
tableview.top …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个 UITableView,其中的单元格具有圆角、边框和缩进。将这三者结合起来,我很难在每种情况下的单元格的侧面和之间获得 1.0pt 的边框(1 个单元格、2 个单元格、> 2 个单元格)。
在最简单的版本中,唯一的问题是单元格之间的边框是双倍宽度。由于borderWidth只能为整个框架设置,我尝试用以下方法添加单独的边框:
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case .top:
border.frame = CGRect(x: 0, y: 0, width: frame.width, height: thickness)
case .bottom:
border.frame = CGRect(x: 0, y: frame.height - thickness, width: frame.width, height: thickness)
case .left:
border.frame = CGRect(x: 0, y: 0, width: thickness, height: frame.height)
case .right:
border.frame = CGRect(x: frame.width - thickness, y: 0, width: thickness, height: …Run Code Online (Sandbox Code Playgroud) 我在用
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 400, height: 600)
}
Run Code Online (Sandbox Code Playgroud)
指定 myCollectionView的像元大小。它工作正常,但是一旦我(UILabel.width == ContentView.width)在单元格UILabel内部添加宽度约束ContentView,单元格的宽度就会缩小到 的固有宽度UILabel。
我正在使用UICollectionViewDelegateFlowLayout水平滚动。
如何强制坚持ContentView我指定的单元格大小并让子视图遵循自动布局约束?
autolayout uicollectionview swift uicollectionviewflowlayout
我在 stackView 中有三个排列的子视图,如下所示:我的“Rec”标签和“00:00:00”标签的底部和顶部部分被剪掉。我不想那样
方形红色视图的约束:高度 = 10px 宽度 = 10px
“Rec”和“00:00:00”标签的约束 - 无
StackView 的约束:' 垂直居中在安全区域尾随空间到安全区域 30px
请忽略白色背景视图,它的存在只是为了让我可以显示我的标签的剪辑......它没有任何限制。
我想要一种 stackViews 高度由“Rec”或“00:00:00”标签的内在内容大小决定的方法。以高度较大者为准。
我创建了一个UIViewRepresentableforUIVisualEffectView以使某些组件充满活力。这是有效的,但是它似乎有时会垂直收缩控件,或者只是在运行时随机改变它们的边界。我似乎无法让它可靠地工作。我需要它来处理任何 SwiftUI 内容,甚至其他UIViewRepresentable用于代替内容的内容。包裹 a 的UIVisualEffectView内部UIView并使用自动布局似乎有帮助,但其他控件(例如UILabel包裹在 a 内部的自定义UIViewRepresnetable会被垂直剪裁)。
public struct VibrantView<Content: View>: UIViewRepresentable {
private let content: UIView!
private let vibrancyBlurEffectStyle: UIBlurEffect.Style
init(vibrancyBlurEffectStyle: UIBlurEffect.Style, @ViewBuilder content: () -> Content) {
self.content = UIHostingController(rootView: content()).view
self.vibrancyBlurEffectStyle = vibrancyBlurEffectStyle
}
public func makeUIView(context: Context) -> UIView {
let containerView = UIView()
let blurEffect = UIBlurEffect(style: vibrancyBlurEffectStyle)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let blurView = UIVisualEffectView(effect: vibrancyEffect)
blurView.translatesAutoresizingMaskIntoConstraints = false …Run Code Online (Sandbox Code Playgroud) 我是 iOS 应用程序开发新手,目前正在研究自动布局。
有时,当我添加约束时,“安全区域”会丢失。
我试图为我的 StackView 添加约束,相对于“安全区域”,前导、尾随、顶部和底部为 0, 0, 0, 0,但由于某种原因,约束选项中缺少“安全区域”。请参阅下面的屏幕截图。
为什么有些开发人员会添加这样的约束:
NSLayoutConstraint(item: myView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 20.0).isActive = true
Run Code Online (Sandbox Code Playgroud)
还有一些像这样:
myView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 20).isActive = true
Run Code Online (Sandbox Code Playgroud)
他们基本上做同样的事情......对吧?那么它们之间有什么区别呢?为什么应该使用其中一种而不是另一种?使用其中一种与另一种相比是否存在性能差异?
在我工作的地方,我们的 iOS 主管专门使用NSLayoutConstraint初始化方式,每个人都被迫这样做,以提高整个代码的一致性和可读性,我喜欢这两种方式,我只是想知道是否有任何初始化方式使用其中一种比另一种有什么好处?或者差异只是基于偏好?
所以我仍然习惯于iOS 6中的自动布局世界,这是一个有趣的(或简单的英语 - 艰难的)迁移来自字符串和struts.
我有一个UIImageView,它是我正在进行的游戏的背景.以下是Interface Builder中常规的3.5英寸Retina显示器:

但如果我将内容视图的模拟指标字段中的"大小"弹出窗口更改为"Retina 4全屏",这就是我所看到的:

你可以看到模拟的iPhone 5屏幕底边出现丑陋的黑条.同样丑陋的黑条使它成为iPhone 5模拟器中运行的编译应用程序.
我是否可以通过Interface Builder应用任何属性或约束来使UIImageView的框架正确适合适当的iPhone设备屏幕尺寸?
或者我是否必须通过代码输入约束?(啊)
我已经观看了三个WWDC视频,如果工程师介绍了调整视图大小适合父母的话题,那么他们必须对它进行快速掩饰,因为我还没有找到或听到一个合适的方法来获取UIImageView和NSImageView在iOS和MacOS端正确调整其父视图的大小.
autolayout ×10
swift ×8
ios ×4
uitableview ×3
xcode ×2
anchor ×1
ios6 ×1
iphone ×1
localization ×1
objective-c ×1
swiftui ×1
ui-design ×1
uiimageview ×1