Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set an absolute item to parent view's center? #71

Closed
darkhandz opened this issue Mar 24, 2018 · 4 comments
Closed

How to set an absolute item to parent view's center? #71

darkhandz opened this issue Mar 24, 2018 · 4 comments
Labels

Comments

@darkhandz
Copy link

Hmmmm....I'm studying this framework, and I don't know how to set the yellow view to the view's center (horizontal & vertical) when it's being position(.absolute) mode, could you please give me some solution? 馃槈

image

import UIKit
import FlexLayout

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupSubviews()
    }

    fileprivate func setupSubviews() {
        let imageView = UIImageView()
        imageView.backgroundColor = .cyan
        
        let seg = UISegmentedControl(items: ["Intro", "FlexLayout", "PinLayout"])
        
        let label1 = UILabel()
        label1.numberOfLines = 0
        label1.text = "FlexBox Layouting is simple, powerful and fast."
        
        let label2 = UILabel()
        label2.numberOfLines = 0
        label2.text = "FlexLayout syntax is concise and chainable."
        
        let label3 = UILabel()
        label3.numberOfLines = 0
        label3.text = "FlexLayout/yoga is incredibly fast, its even faster than manual layout."
        
        
        view.flex.padding(10).addItem().direction(.row).define { flex in
            flex.addItem(imageView).width(100).height(80)
            flex.addItem().marginLeft(10).shrink(1).define({ flex in
                flex.addItem(seg)
                flex.addItem(label1).marginTop(15)
                flex.addItem(label2).marginTop(15)
            })
        }
        // separator
        view.flex.addItem().marginTop(15).height(1).backgroundColor(.lightGray)
        view.flex.addItem(label3).marginTop(15)
        
        // absolute
        view.flex.addItem().size(100).position(.absolute).alignSelf(.center).top(200).backgroundColor(.yellow)
    }    
   
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        view.flex.layout()
    }
}
@linqingmo
Copy link

.top(50%).marginTop(-50)

@darkhandz
Copy link
Author

darkhandz commented Mar 24, 2018

@linqingmo I've try your code, but I got a compile error, .top() don't have percent overload. 馃槆

@linqingmo
Copy link

Sorry, I forgot. I add it myself.

@discardableResult
public func top(_ percent: FPercent) -> Flex {
    yoga.top = YGValue(value: Float(percent.value), unit: .percent)
    return self
}

@darkhandz
Copy link
Author

@linqingmo thanks, it works. Hope framework will add this convenience func. 馃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants