diff --git a/Example/FramezillaExample/ViewController.swift b/Example/FramezillaExample/ViewController.swift index 5d42f83..d1b1b05 100644 --- a/Example/FramezillaExample/ViewController.swift +++ b/Example/FramezillaExample/ViewController.swift @@ -21,13 +21,18 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + + scrollView.backgroundColor = .yellow + scrollView.contentSize = CGSize(width: 500, height: 1000) + content1.backgroundColor = .red content2.backgroundColor = .green content3.backgroundColor = .black - view.addSubview(content1) - view.addSubview(content2) - view.addSubview(content3) + view.addSubview(scrollView) + scrollView.addSubview(content2) + +// view.addSubview(content3) } override func viewDidLayoutSubviews() { @@ -48,20 +53,31 @@ class ViewController: UIViewController { // maker.centerY(between: content2, content1) // maker.centerX() // } - - content1.configureFrame { maker in - maker.edges(top: 0, left: 0, bottom: 0) - maker.width(50) + + +// content1.configureFrame { maker in +// maker.edges(top: 0, left: 0, bottom: 0) +// maker.width(50) +// } +// +// content2.configureFrame { maker in +// maker.edges(top: 0, bottom: 0, right: 0) +// maker.width(200) +// } +// +// content3.configureFrame { maker in +// maker.size(width: 20, height: 70) +// maker.centerX(between: content1, content2) +// maker.centerY() +// } + + scrollView.configureFrame { maker in + maker.margin(20) } - + content2.configureFrame { maker in - maker.edges(top: 0, bottom: 0, right: 0) - maker.width(200) - } - - content3.configureFrame { maker in - maker.size(width: 20, height: 70) - maker.centerX(between: content1, content2) + maker.size(width: 100, height: 100) + maker.left() maker.centerY() } } diff --git a/Framezilla.podspec b/Framezilla.podspec index 1f4f397..1a527a9 100644 --- a/Framezilla.podspec +++ b/Framezilla.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "Framezilla" - spec.version = "2.2.0" + spec.version = "2.2.1" spec.summary = "Comfortable syntax for working with frames." spec.homepage = "https://github.com/Otbivnoe/Framezilla" diff --git a/Sources/MakerHelper.swift b/Sources/MakerHelper.swift index 9f23ccd..d851bae 100644 --- a/Sources/MakerHelper.swift +++ b/Sources/MakerHelper.swift @@ -26,16 +26,18 @@ fileprivate extension UIView { extension Maker { func convertedValue(for type: RelationType, with view: UIView) -> CGFloat { - var convertedRect: CGRect { - if let superScrollView = self.view.superview as? UIScrollView, view is UIScrollView { - return CGRect(x: 0, - y: 0, - width: superScrollView.contentSize.width, - height: superScrollView.contentSize.height) - } - else { - return self.view.superview!.convert(view.frame, from: view.superview) + + var rect: CGRect { + if let superview = self.view.superview, superview === view, superview.superview == nil { + return CGRect(origin: .zero, size: superview.frame.size) } + return self.view.superview!.convert(view.frame, from: view.superview) + } + + var convertedRect = rect + if let superScrollView = self.view.superview as? UIScrollView, view is UIScrollView, superScrollView.contentSize != .zero { + convertedRect.size.width = superScrollView.contentSize.width + convertedRect.size.height = superScrollView.contentSize.height } switch type { diff --git a/Tests/ScrollViewTests.swift b/Tests/ScrollViewTests.swift index 4ac8402..3802375 100644 --- a/Tests/ScrollViewTests.swift +++ b/Tests/ScrollViewTests.swift @@ -17,7 +17,7 @@ final class ScrollViewTests: XCTestCase { scrollView.contentSize = CGSize(width: 300, height: 300) } - func testThanCorrectlyConfigures_edges_relativelyScrollView() { + func testThanCorrectlyConfigures_edges_relativelyScrollViewWithNonZeroContentSize() { let view = UIView(frame: .zero) scrollView.addSubview(view) @@ -29,7 +29,21 @@ final class ScrollViewTests: XCTestCase { XCTAssertEqual(view.frame, CGRect(x: 10, y: 20, width: 260, height: 240)) view.removeFromSuperview() } - + + func testThanCorrectlyConfigures_edges_relativelyScrollViewWithZeroContentSize() { + scrollView.contentSize = .zero + + let view = UIView(frame: .zero) + scrollView.addSubview(view) + + view.configureFrame { maker in + maker.edges(top: 20, left: 10, bottom: 40, right: 30) + } + + XCTAssertEqual(view.frame, CGRect(x: 10, y: 20, width: 60, height: 40)) + view.removeFromSuperview() + } + func testThanCorrectlyConfigures_center_relativelyScrollView() { let view = UIView(frame: .zero) @@ -44,7 +58,7 @@ final class ScrollViewTests: XCTestCase { view.removeFromSuperview() } - func testThanCorrectlyConfiguresFewSubviewTogeter() { + func testThanCorrectlyConfiguresFewSubviewTogether() { let view1 = UIView(frame: .zero) let view2 = UIView(frame: .zero)