Thanks, wang_stg.
Your solution worked for me.
Here is swift version of the code: (SnapKit is used for defining constraints)
import UIKit
enum BarButtonPosition {
case left
case right
}
public class BarButton: UIButton {
var position: BarButtonPosition! = .left
private var applied: Bool = false
override public func layoutSubviews() {
super.layoutSubviews()
let os_version = OperatingSystemVersion(majorVersion: 11, minorVersion: 0, patchVersion: 0)
if applied || !ProcessInfo.processInfo.isOperatingSystemAtLeast(os_version) {
return
}
var view: UIView! = self
while (!view.isKind(of: UINavigationBar.self) && view.superview != nil) {
if let stackView = view.superview {
if (stackView.isKind(of: UIStackView.self) && stackView.superview != nil) {
if self.position == .left {
stackView.snp.makeConstraints({ (make) in
make.leading.equalToSuperview().offset(8)
})
applied = true
} else if self.position == .right {
stackView.snp.makeConstraints({ (make) in
make.trailing.equalToSuperview().offset(-8)
})
applied = true
}
break
}
view = stackView
}
}
}
}