我有一个CGRect
,我想用UIEdgeInsets
进行调整。
似乎可能有一个内置函数可以做到这一点。我找了CGRectAdjustByInsets
或其他CGRect…
前缀的函数,但我找不到任何内容。
我应该自己编码吗?
答案 0 :(得分:88)
Swift 4.2 使用theRect.inset(by: theInsets)
。
目标c 使用UIEdgeInsetsInsetRect(theRect, theInsets)
// CGRectMake takes: left, bottom, width, height.
const CGRect originalRect = CGRectMake(0, 0, 100, 50);
// UIEdgeInsetsMake takes: top, left, bottom, right.
const UIEdgeInsets insets = UIEdgeInsetsMake(10, 10, -20, -20);
// Apply the insets…
const CGRect adjustedRect = UIEdgeInsetsInsetRect(originalRect, insets);
// What's the result?
NSLog(@"%@ inset by %@ is %@",
NSStringFromCGRect(originalRect),
NSStringFromUIEdgeInsets(insets),
NSStringFromCGRect(adjustedRect));
// Logs out…
// {{0, 0}, {100, 50}} inset by {10, 10, -20, -20} is {{10, 10}, {110, 60}}
CGRect
涵盖了在{{1}}上运行的其他有用功能。
答案 1 :(得分:8)
说你想要界限,
但是例如底部上的两个像素更少:
let ei = UIEdgeInsetsMake(0, 0, 2, 0) // top-left-bottom-right
let smaller = UIEdgeInsetsInsetRect(bounds, ei)
那就是它。
从底部取下两个:
let newBounds = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 2, 0))
干杯
答案 2 :(得分:6)
我想新方法看起来更好...
let newCGRect = oldCGRect.inset(by: UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8))
答案 3 :(得分:0)
CGRect insetRect = CGRectInset(rOriginalRect, fInsetX, fInsetY);