Using iOS 10.20 & Swift 3.0 Want to use this excellent piece of code written by Stephen Poletto a while back in my code, but need it in Swift 3.0 really.
Almost there but I am stuck right now. My code crashes cause it doesn't execute setContentView or setFrame, and I don't understand how Stephan's code calls this.. I see no reference to either of there methods in his code? do they called when he sets a frame or view perhaps? What is the Swift equivient? I recoded the methods, but their not be executed...
- (void)setContentView:(UIView *)newContentView { [contentView removeFromSuperview]; contentView = newContentView; contentView.frame = CGRectInset(self.bounds, kSPUserResizableViewGlobalInset + kSPUserResizableViewInteractiveBorderSize/2, kSPUserResizableViewGlobalInset + kSPUserResizableViewInteractiveBorderSize/2); [self addSubview:contentView]; // Ensure the border view is always on top by removing it and adding it to the end of the subview list. [borderView removeFromSuperview]; [self addSubview:borderView]; } - (void)setFrame:(CGRect)newFrame { [super setFrame:newFrame]; contentView.frame = CGRectInset(self.bounds, kSPUserResizableViewGlobalInset + kSPUserResizableViewInteractiveBorderSize/2, kSPUserResizableViewGlobalInset + kSPUserResizableViewInteractiveBorderSize/2); borderView.frame = CGRectInset(self.bounds, kSPUserResizableViewGlobalInset, kSPUserResizableViewGlobalInset); [borderView setNeedsDisplay]; } My Swift 3.0 code looks like this?
func setContentView(newContentView: UIView) { contentView.removeFromSuperview() contentView = newContentView contentView.frame = self.bounds.insetBy(dx: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0, dy: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0) self.addSubview(contentView) // Ensure the border view is always on top by removing it and adding it to the end of the subview list. borderView.removeFromSuperview() self.addSubview(borderView) } func setFrame(newFrame: CGRect) { contentView.frame = self.bounds.insetBy(dx: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0, dy: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0) borderView.frame = self.bounds.insetBy(dx: CGFloat(kUserResizableViewGlobalInset), dy: CGFloat(kUserResizableViewGlobalInset)); borderView.setNeedsDisplay() } 1 Answer
Ok,
Here is a partial answer, this is how you override setFrame in Swift it seems; found the answer on reddit.
override var frame: CGRect { get { return super.frame } set (newFrame) { super.frame = newFrame contentView.frame = self.bounds.insetBy(dx: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0, dy: CGFloat(kUserResizableViewGlobalInset) + kUserResizableViewInteractiveBorderSize / 2.0) borderView.frame = self.bounds.insetBy(dx: CGFloat(kUserResizableViewGlobalInset), dy: CGFloat(kUserResizableViewGlobalInset)); borderView.setNeedsDisplay() } } At least it partly works. I am step closer! Would use the same method for setContentView but figuring out the syntax on that is still eluding me right now.