Post

Replies

Boosts

Views

Activity

swift and AppDirectory
I develop an App in macos, and I need to access the documentDirectory: let dialogURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first I get: file:///Users//Library/Containers/<devTeam.appName>/Data/Documents But in the Library/Containers directory I don't have the <devTeam.appDirectory> (in my case "PR.medical") but (in my case "medical") of course, it would be possible to hard code the name of the desired directory, but if in the future apple change anything, my app will not work properly. Am I missing something?
4
0
1.1k
Nov ’21
Interface Builder and subclassed UIView
I test a very simple thing: I have a Subclassed UIView as follow: @IBDesignable class myView: UIView {          override public init(frame: CGRect) {         super.init(frame: frame)     }     required public init?(coder aDecoder: NSCoder) {         super.init(coder: aDecoder)     }       override open func layoutSubviews() {         super.layoutSubviews()         layer.frame.size.height = 50         layer.borderWidth = 1         layer.borderColor = UIColor.green.cgColor     } } To install this component in Interface Builder, I add an UIView and I change the class name in the Inspector. When I add the UIView, the height of this view is 128. I suppose it is a default value of IB. But I want this height to be 50. When I change the name of the class in the IB inspector, the view is correctly drawn, white a height of 50, but when I select it, the size of the selection is the size of a default UIView Is there a mean to correct this?
3
0
789
Sep ’21
programmatic constraints and Interface Builder
I subclassed an UIView that is supposed to create it's owned constraints: @IBDesignable class MyCtrl: UIView { var label: UITextField! var _internalLayout = false public override init(frame: CGRect) { super.init(frame: frame) _init() } public required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) _init() } open override func prepareForInterfaceBuilder() { super.prepareForInterfaceBuilder() self._init() self.setNeedsDisplay() } func _init() { label = UITextField() addSubview(label) super.backgroundColor = .cyan #if TARGET_INTERFACE_BUILDER translatesAutoresizingMaskIntoConstraints = true #else translatesAutoresizingMaskIntoConstraints = false #endif } override public func layoutSubviews() { if _internalLayout { _internalLayout = false return } let theOrientation = orientation setupConstraints(theOrientation) super.layoutSubviews() } func setupConstraints(_ orientation: IosTraitClass) { _internalLayout = true superview?.removeConstraints(superview!.constraints) if orientation == .CR { label.text = "setupConstraints(.CR \(superview?.frame.size.width)" label.sizeToFit() superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: superview , attribute: .leading , multiplier: 1, constant: 30)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: superview, attribute: .top, multiplier: 1, constant: 100)) superview?.addConstraint( NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.width ?? 0)-60)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.height ?? 0)-300)) } else if orientation == .RC || orientation == .CC { label.text = "setupConstraints(.CC ou .RC \(superview?.frame.size.width)" label.sizeToFit() superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: superview, attribute: .trailing, multiplier: 1, constant: -50)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: superview, attribute: .top, multiplier: 1, constant: 50)) superview?.addConstraint( NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.width ?? 0)-100)) superview?.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: .none, attribute: .notAnAttribute, multiplier: 1, constant: (superview?.frame.size.height ?? 0)-100)) } } } In the method layoutSubviews of my component, I called orientation which is an extension of UIView: public var orientation: IosTraitClass { if UIScreen.main.traitCollection.horizontalSizeClass == .regular && UIScreen.main.traitCollection.verticalSizeClass == .compact { return .RC } else if UIScreen.main.traitCollection.horizontalSizeClass == .compact && UIScreen.main.traitCollection.verticalSizeClass == .regular { return .CR } else if UIScreen.main.traitCollection.horizontalSizeClass == .compact && UIScreen.main.traitCollection.verticalSizeClass == .compact { return .CC } else if UIScreen.main.traitCollection.horizontalSizeClass == .regular && UIScreen.main.traitCollection.verticalSizeClass == .regular { return .RR } else { return .none } } Finally I made a UIViewController: class ViewController: UIViewController { @IBOutlet weak var container: MyCtrl! override func viewDidLoad() { super.viewDidLoad() setupConstraints() } func setupConstraints() { container.setupConstraints(view.orientation) } func calculatedConstraints() -> [NSLayoutConstraint] { return []; var constraints = [NSLayoutConstraint]() container.removeConstraints(container.constraints) if view.orientation == .CR { //constraints.append(contentsOf: container.setSize(width: (view?.frame.size.width ?? 0)-60, height: (view?.frame.size.height ?? 0)-300)) } else { //constraints.append(contentsOf: container.setSize(width: (view?.frame.size.width ?? 0)-40, height: (view?.frame.size.height ?? 0)-100)) } return constraints } override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { container.setupConstraints(view.orientation) super.viewWillTransition(to: size, with: coordinator) } } When I run my program the constraints are well applied in landscape or portrait mode. But when I look in the interface Builder, constraints are not applied, and I don't understand Why
2
0
1.1k
Aug ’21