Auto Layout

RSS for tag

Create a user interface that dynamically responds to changes in the available screen space using Auto Layout.

Posts under Auto Layout tag

117 Posts

Post

Replies

Boosts

Views

Activity

How to set segmented picker style to use the full width in iOS 17?
Before iOS 17 beta, the .pickerStyle(.segmented) modifier makes the Picker take up the entire available width in a toolbar. In iOS 17, it tightly fits the width of the text. I can use .fixedSize() modifier to make the segmented control tightly fit the content. In reverse, what is the recommended way to make it expand to fill the whole width? In iOS 17 beta, I tried .frame(idealWidth: UIScreen.main.bounds.width) and it works, but it doesn't have effect in iOS 16 and prior. Also, it is semantically wrong to set the ideal width to an arbitrary big value. Example: iOS 16, full iOS 17, tight
3
0
2.1k
Aug ’23
IOS UIkit ViewDidLayout Calls On Handle Drag
I am a new uikit developer. I have a problem on my app. App has 2 different parts. Rectangle view and button. Rectangle view can drag with pan touch gesture. Button can change own image with tap gestures. When user taps the button, it changes own image. But when I tap the button, rectangle view moves to the at the beginning position.(view did load position). I checked logs and I see viewDidLayout methods calling when I tap the button. How can I handle this problem, is there anyone help me ? Thanks a lot. import UIKit final class SampleViewController: UIViewController { private var isTapped: Bool = false let button: UIButton = { let button = UIButton() let image = UIImageView(image: UIImage(systemName: "play")) button.translatesAutoresizingMaskIntoConstraints = false button.backgroundColor = .yellow return button }() let littleView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.layer.borderColor = UIColor.red.cgColor view.layer.borderWidth = 4 view.isUserInteractionEnabled = true view.backgroundColor = .red return view }() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(button) self.view.addSubview(littleView) littleView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) NSLayoutConstraint.activate([ button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), littleView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/5), littleView.widthAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/3), ]) } override func viewDidLayoutSubviews() { littleView.frame = CGRect(x: 0, y: 0, width: littleView.frame.width, height: littleView.frame.height) } override func viewWillLayoutSubviews() { littleView.frame = CGRect(x: 0, y: 0, width: littleView.frame.width, height: littleView.frame.height) } @objc private func buttonTapped() { button.setImage(UIImage(systemName: isTapped ? "play.slash" : "play"), for: .normal) self.view.layoutIfNeeded() isTapped.toggle() print("Button tapped") } @objc private func handlePan(_ gesture: UIPanGestureRecognizer) { guard let viewToMove = gesture.view else { return } if gesture.state == .began || gesture.state == .changed { let translation = gesture.translation(in: view) let newX = max(0, min(view.frame.width - viewToMove.frame.width, viewToMove.frame.origin.x + translation.x)) let newY = max(0, min((view.frame.height - viewToMove.frame.height), viewToMove.frame.origin.y + translation.y)) viewToMove.frame.origin = CGPoint(x: newX, y: newY) gesture.setTranslation(CGPoint.zero, in: view) } } } Github link
1
0
805
Aug ’23
App crashing randomly - Not possible to remove variable
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Not possible to remove variable: 400: unknown var (bug!) with engine as delegate:0x283c8ed40{id: 33112} colIndex:32 from engine NSISEngine: 0x129248130{ delegate:0x12934eaa0 EngineVars:  0: objective{id: 33102} rowIndex:0  1: UIImageView:0x12924ec90.Height{id: 32592} rowIndex:1073741824  2: 0x280a04be0.marker{id: 32591} colIndex:1073741825  3: UIImageView:0x12924ec90.Width{id: 32807} rowIndex:1  4: 0x280dc35a0.posErrorMarker{id: 32871} colIndex:1  5: 0x280dc35a0.negError{id: 32872} rowIndex:88  6: 0x280dc3d80.posErrorMarker{id: 32873} colIndex:1073741826  7: 0x280dc3d80.negError{id: 32874} rowIndex:1073741825  8: UIImageView:0x129353f00.Width{id: 32594} rowIndex:2  9: 0x280979d60.marker{id: 32593} colIndex:3 10: 0x280dc1e60.posErrorMarker{id: 32875} colIndex:4 11: 0x280dc1e60.negError{id: 32876} rowIndex:3 12: UIImageView:0x129353f00.Height{id: 32822} rowIndex:1073741826 13: 0x280dc39c0.posErrorMarker{id: 32877} colIndex:1073741827 14: 0x280dc39c0.negError{id: 32878} rowIndex:1073741898 15: UIButton:0x1293540d0.Width{id: 32817} rowIndex:4 16: 0x280dc2040.posErrorMarker{id: 32879} rowIndex:93 17: 0x280dc2040.negError{id: 32880} colIndex:6 18: UIButton:0x1293540d0.Height{id: 32820} rowIndex:1073741827 19: 0x280dc3de0.posErrorMarker{id: 32881} rowIndex:96 20: 0x280dc3de0.negError{id: 32882} colIndex:1073741830 21: UILabel:0x129353300.Width{id: 32811} rowIndex:5 22: 0x280dc3e40.posErrorMarker{id: 32883} colIndex:44 23: 0x280dc3e40.negError{id: 32884} rowIndex:90 24: UILabel:0x129353300.Height{id: 32842} colIndex:1073741828 25: 0x280dc27c0.posErrorMarker{id: 32885} colIndex:1073741831 26: 0x280dc27c0.negError{id: 32886} rowIndex:1073741828 27: UILabel:0x129353c90.Width{id: 32840} rowIndex:6 28: 0x280dc1da0.posErrorMarker{id: 32887} colIndex:9 29: 0x280dc1da0.negError{id: 32888} rowIndex:97 30: UILabel:0x129353c90.Height{id: 32829} colIndex:1073741832 31: 0x280dc31e0.posErrorMarker{id: 32889} colIndex:1073741833 32: 0x280dc31e0.negError{id: 32890} rowIndex:1073741900 33: UIImageView:0x12934e490.Width{id: 32596} rowIndex:1073741905 34: UIImageView:0x12934e490.Height{id: 32597} colIndex:1073741844 35: 0x280963a70.marker{id: 32595} colIndex:11 36: 0x280dc2ee0.posErrorMarker{id: 32891} colIndex:12 37: 0x280dc2ee0.negError{id: 32892} rowIndex:8 38: 0x280dc1d40.posErrorMarker{id: 32893} colIndex:1073741835 39: 0x280dc1d40.negError{id: 32894} rowIndex:1073741830 40: UIButton:0x1293543b0.Width{id: 32832} rowIndex:9 41: 0x280dc1440.posErrorMarker{id: 32895} colIndex:0 42: 0x280dc1440.negError{id: 32896} rowIndex:1073741845 43: UIButton:0x1293543b0.Height{id: 32827} rowIndex:1073741831 44: 0x280dc3840.posErrorMarker{id: 32897} colIndex:1073741836 45: 0x280dc3840.negError{id: 32898} rowIndex:1073741829 46: UILabel:0x129353a20.Width{id: 32848} rowIndex:10 and another thousand or so lines. this crash occurs after I change the language and open the workout page to start a workout immediately, and sometimes on the second time, I open the workout page.
10
0
7.6k
Jul ’23
Constrain view's top anchor to just at the edge of sensor housing
What might be a good way to constrain a view's top anchor to be just at the edge of a device's Face ID sensor housing if it has one? This view is a product photo that would be clipped too much if it ignored the top safe area inset, but if it was positioned relative to the top safe area margin this wouldn't be ideal either because of the slight gap between the sensor housing and the view (the view is a photo of pants cropped at the waist). What might be a good approach here?
0
0
1.1k
Jul ’23
App crashing for users, coreautolayout issue with error negativeErrorVarForBrokenConstraintWithMarker
Hi All, Our crash reporting solution show app crashes for the user with crash reports which are not getting symbolicating to anything useful. it just says, App crashed on thread 0 (CoreAutoLayout -[NSISEngine negativeErrorVarForBrokenConstraintWithMarker:errorVar:] with it ending up on main.swift file. We have not able to replicate at our end, and it is happening only for a small fraction of our user (< 1%). This issue is spread across multiple iOS versions and device models. We have verified all our UI actions are being done on main thread. Any pointers for solving this are welcome. Regards, Ishan
0
2
1.3k
Jun ’23
Stage manager safe area and hidden status bar: How to avoid overlapping by stage manager controls?
Stage manager controls are overlapping top area of an app if the status bar is hidden despite constraints are relative to the safe area. It is surprise for me that the stage manager is not included in the safe area and designed that the status bar will be responsible for making enough margin to avoid overlapping. Is there anything I am missing on how to treat such problem except avoiding controls in the top area at all and enabling the status bar in the app (users got used to it and we don't see it is possible to explain such changes to all of them)? I see that safe are is disrespected, and I do not find any helpful api or settings in interface builder. The only patch I have right now is to auto hide home indicator override func viewDidLoad() { super.viewDidLoad() setNeedsStatusBarAppearanceUpdate() } override var prefersHomeIndicatorAutoHidden: Bool { return true } The stage manager controls are hidden with home indicator until first user interaction. This might help just a little to have at least possibility click something in the top center once in a while. But overall experience is bad. Steps: Create a new project for iOS in Xcode using storyboard as interface. Add a button or any other view/control to the main view controller view in interface builder. Add the constraints to connect the button with its parent viewcontroller’s view: the top constraint should be related to top safe area with 0 space; centerX of the button aligned with parent’s centerX. In main view controller override prefersStatusBarHidden to return true. override var prefersStatusBarHidden: Bool { get { return true } Launch the app and try to click created button. Expected: Created button is clickable and placed slightly below the stage manger’s controls. Safe area does respect the stage manager controls even if the status bar is hidden the same way it behaves in case of avoiding iPhone notches. Or at least there is any API to check if the stage manager is enabled. Actual: Created button is overlapped by the stage manager’s controls and not clickable.
0
1
1.1k
May ’23
UIViewPropertyAnimator: "Fast" timing parameters and/or low animation duration causes scrubbing to snap between values at the start
I've been playing around with the awesome UIViewPropertyAnimator, but are observing something during scrubbing I don't quite understand. Linearly scrubbing a UIViewPropertyAnimator configured with "fast" timing parameters and/or low animation duration, causes the animated properties to snap between (larger) values at the start of scrubbing. This can be made visible with a simple bottom sheet example (recorded on iPhone 14 Pro with iOS 16.4.1): Small/"fast" timing parameters: Large/"slow" timing parameters: The UIViewPropertyAnimator is instantiated as follows (the issue also observed with other initializers): // duration set to 0.25 gives the unwanted snapping at the start of scrubbing: UIViewPropertyAnimator(duration: 0.25, dampingRatio: 1) // duration set to 1.25 gives the expected smooth scrubbing. UIViewPropertyAnimator(duration: 1.25, dampingRatio: 1) Any ideas about what's going on here? I would expect the scrubbing to be smooth independent of what parameters are given? It's especially odd that the snapping only happens at the start of the scrubbing... Also attached the full example code: BottomSheetAnimationViewController.txt
0
0
1.3k
May ’23
Problem with NSTableView constraints
Hi, the problem is not dangerous to life, I don't want see the yellow button on top of the editor. 'Autoresizing Mask' is set to about 50 items, that works, except tableView, 10 warnings. The tableView has 10 columns of 20 pixels width, min 10 and max 100000. The ' Add new constraints' options are all grayed, so what ? In an other project a warning was set for arrayController, I don't like Auto Layout, the blue help lines are enough. My question is : How do i can suppress this warnings ? Kindly, Uwe
0
0
1.1k
May ’23
How does NSStackView effect NSTableView.bounds rowAtPoint and NSEvent.locationInWindow
I'm trying to get the row index of a NSTableRowView that has experienced a mouseEnter event. It should be easy with rowAtPoint using NSEvent.locationInWindow. I can't get it to work. The NSTableView lies inside a NSStackView. Of course the NSStackView adjusts the visual size of the NSTableView etc. Is NSStackView interfering? If so, how should I adjust my call to rowAtPoint?
0
0
844
Apr ’23
Notification dot for camera and mic doesn’t stay inside the Dynamic island
hello Developers, im using Iphone 14 pro max and found out that when the Device camera or mic is in use then a notification dot can be seen in the Dynamic island. But if the notification dot is there and we perform another action related to Dynamic island. Like set a Timer or Connect the device with charger. Then the notication dot from middle goes all the way to the right side of the screen. Doesn’t it should just stay in the middle? As none of the other dynamic island related actions consumes that dead pixel part of screen.
2
0
854
Apr ’23
Adding and positioning views to UITableViewCell.contentView
Hi there, the documentation explicitly says this is what's contentView for. I've just tried and found big problems not adding — works like a charm — but positioning my view so that it does not clash with the standard ones. I've added a small badge at the rightmost edge of contentView. Having added the subview into contentView (in tableView:cellForRowAtIndexPath:, after checking that it does not exist yet in a reused cell view), I set up constraints so that it is Y-centered and sticks to the right edge of the contentView, that was very easy and worked nicely at the first try: UILayoutGuide *lg=cell.contentView.layoutMarginsGuide; [badge.trailingAnchor constraintEqualToAnchor:lg.trailingAnchor].active=YES; [badge.centerYAnchor constraintEqualToAnchor:lg.centerYAnchor].active=YES; I've found though if the text in textLabel (I have to address iOS12+, thus I can't use content configurations) is long enough, it clashes with the badge instead of clipping/wrapping/whatever it is set to (wrapping in my case). Thus I've added another constraint which makes sure the trailing edge of textLabel does not exceed the leading edge of my badge: UILayoutGuide *blg=badge.layoutMarginsGuide; [cell.textLabel.trailingAnchor constraintLessThanOrEqualToAnchor:blg.leadingAnchor constant:-6].active=YES; Oops: that pushed the badge right, so that it overlaps the accessory area. By my further testing, seems the culprit is another constraint (probably created dynamically later from the autoresizing mask) which bounds the right edge of textLabel to the right edge of contentView. I've tried to find and remove (deactivate) it in tableView:cellForRowAtIndexPath:, in vain, well, makes some sense perhaps, too soon. I've tried to find and remove it in tableView:willDisplayCell:forRowAtIndexPath:; no luck either. To me, this does not make sense at all. (It sort of looks like the documentation claim “After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out” is highly overstated. Self-evidently, non-trivial layout stuff happens after the delegate returns, but before the cell is drawn.) Eventually, having used updateConstraints explicitly (which does not feel right), I succeeded to find the darn thing, a fixed width constraint for textLabel. Tried to deactivate it: does not work, setting its active property to NO does nothing, no warning, no error, no exception, thing just stays at YES. I've tried to decrease its priority, that's even more funny: it goes down, when checked, it is down, but 1000 is somehow re-surrected after tableView:willDisplayCell:forRowAtIndexPath: returns, but before the cell is finally laid out and drawn. Ick. What's the proper solution of this? I suppose it should be pretty easy, but it is not, or I must be overlooking something pretty obvious. Thanks!
0
0
1.1k
Mar ’23
NSISEngine Auto Layout Crash (Not possible to remove variable)
Hey there, We're experiencing a big bump of these crashes with no great explanation. We're not doing anything special about the layout there except for using some nested Stack Views, and the view itself is embedded within a reusable cell. Attaching a full crash log - have plenty more of these but they are quite similar. Would appreciate your help :) Shai. 2021-10-20_11-48-29.1535_+0300-c0ce73e85344ddcdc178995f3e83de0b406bab54.crash
4
2
3.6k
Mar ’23
Interface Builder changes x, y, width and height values randomly.
I have old projects without autolayout. When I open the storyboard in Interface Builder everything looks fine. But if I move a button or label by one pixel, for example, or add a new UIView, then the Interface Builder randomly changes the x, y, width, or height values for almost all elements in all UIViewControllers in the storyboard file. Not all values are changed, but a lot. If I close and open Storyboard a few times and do an action, the values are changed so many times that the user interface is completely broken. Does anyone have an idea how I can fix the problem? I am using Xcode 14.0.1 and it happens with all projects I have. The changes look like this, for example:
10
1
3.1k
Feb ’23
Precompute the height of a UITableView with dynamically sized cells
Howdy, I have a UITableView which is embedded deeply (i.e. nested in other views) within another UIScrollView for reasons. It seems that in order to get this outer UIScrollView to scroll properly, I have to constrain the height of this UITableView ahead of time. My problem is that I can't figure out how to calculate this height quickly with dynamically sized cells (thus contentSize and related methods won't work). The best method I have been able to come up with is to scroll to each cell individually and fetch their height ahead of time sometime during/after layout, which is less than ideal... I imagine I could also adjust the height as cells are being loaded, but that doesn't sound like good user experience (scroll view keeps growing, wouldn't it be choppy?). On quick investigation, it seems that a regular (i.e. non-embedded) UITableView is able to get an accurate height of dynamically sized cells ahead of time (as seen by the scroll bar). I'm curious if this means that there is something I am missing? I'm sure there is a lot left for me to learn. I will appreciate any help. Thanks, smkuehnhold
1
0
2.3k
Jan ’23
Got crashed in my App
got warnings as below every time I perform segue: [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x10bf01280>, navigationBar=<UINavigationBar: 0x10bab7c80; frame = (0 0; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x118e83f80>> delegate=0x10bf01280 Seems it does not direct crash my App directly since I always got this warning after a new iOS (forgot which version). But I got a crash as the attached report, not sure any relation with this warning, or due to another cause, thank you for your help. 2023-01-12, 05-24.crash
0
0
1.4k
Jan ’23
Safe area guide top anchor lost after force change orientation
Hi, I wrote a simple auto layout constraint below and used a force change orientation method to present a full-screen landscape view controller. imageView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 16) After the landscape view controller is dismissed, the safe area guide seems broken, the top safe area insets change to zero, and call layoutIfNeeded() won't fix this issue. The only way to fix this is back to Home Screen and reopen the app. This problem occurred on the iPhone SE simulator, newer devices with a notch don't have this issue. Does anybody know how to fix this? func forceOrientation(orientation: UIInterfaceOrientation) {     let value = orientation.rawValue     if #available(iOSApplicationExtension 16.0, iOS 16.0, *) {         let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene         var interfaceOrientations: UIInterfaceOrientationMask = []         switch orientation {         case .portrait:             interfaceOrientations = .portrait         case .landscapeLeft:             interfaceOrientations = .landscapeLeft         case .landscapeRight:             interfaceOrientations = .landscapeRight         case .portraitUpsideDown:             interfaceOrientations = .portraitUpsideDown         case .unknown:             interfaceOrientations = .portrait         @unknown default:             interfaceOrientations = .portrait         }         windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: interfaceOrientations))     } else {         UIDevice.current.setValue(value, forKey: "orientation")         UINavigationController.attemptRotationToDeviceOrientation()     } }
0
0
1.5k
Jan ’23
Enable Inspector in WKWebView under SwiftUI & SplitView
I am running this on MacOS, Xcode version 11.6. In order to run this code you must add com.apple.security.network.client with the value true to your entitlements file. I want to enable the Inspector for WKWebView. I have created an NSViewRepresentable to encapsulate the WKWebView. Here is the simplified code: struct WKWebViewRepresentable: NSViewRepresentable { &#9;&#9;var urlToDisplay: URL? &#9;&#9;var enableDeveloperTools: Bool = false &#9;&#9;func loadData(wkWebView: WKWebView) { &#9;&#9;&#9;&#9;if let url = urlToDisplay { &#9;&#9;&#9;&#9;&#9;&#9;wkWebView.load(URLRequest(url: url)) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;func makeNSView(context: Context) -> WKWebView { &#9;&#9;&#9;&#9;let wkWebView = WKWebView() &#9;&#9;&#9;&#9;wkWebView.configuration.preferences.setValue(enableDeveloperTools, forKey: "developerExtrasEnabled") &#9;&#9;&#9;&#9;loadData(wkWebView: wkWebView) &#9;&#9;&#9;&#9;return wkWebView &#9;&#9;} &#9;&#9;func updateNSView(_ wkWebView: WKWebView, context: Context) { &#9;&#9;&#9;&#9;loadData(wkWebView: wkWebView) &#9;&#9;} &#9;&#9;func makeCoordinator() -> Coordinator { &#9;&#9;&#9;&#9;return Coordinator(self) &#9;&#9;} } extension WKWebViewRepresentable { &#9;&#9;class Coordinator: NSObject { &#9;&#9;&#9;&#9;init(_ parent: WKWebViewRepresentable) { &#9;&#9;&#9;&#9;&#9;&#9;super.init() &#9;&#9;&#9;&#9;} &#9;&#9;} } and with this code in place I can create the following ContentView: struct ContentView: View { &#9;&#9;static let demoURL = "https://" + "www.apple.com/apple-music/" &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;WKWebViewRepresentable(urlToDisplay: URL(string: ContentView.demoURL), enableDeveloperTools: true) &#9;&#9;&#9;&#9;&#9;&#9;WKWebViewRepresentable(urlToDisplay: URL(string: ContentView.demoURL), enableDeveloperTools: false) &#9;&#9;&#9;&#9;} &#9;&#9;} } The good news is that the above code actually works. If you right click on the left hand side, you can select Inspect Element which will start the Inspector. The idea is that I would do discovery on the left hand side and do show the processed result on the right hand side. That part is obviously not in the code. For testing purposes the right hand side simply shows the same content as the left hand side. I would like to see that the user is being able to split the real-estate between the two views, hence I replace the HStack with HSplitView, no other change. When you run this code, the WkWebView's come up as before but one you select Inspect Element the place where the Inspector should show is blank (black or white depending on dark mode), it starts flashing etc. By dragging inside that rectangle, it becomes clear that the code is actually running just not properly rendering. This stackoverflow - https://stackoverflow.com/questions/60727065/wkwebview-web-inspector-in-macos-app-fails-to-render-and-flickers-flashes article, mentions the same problem and it suggests that autolayout should be disabled. Does anyone know a trick to circumvent this layout problem for SwiftUI?
1
0
4.6k
Dec ’22
How to set segmented picker style to use the full width in iOS 17?
Before iOS 17 beta, the .pickerStyle(.segmented) modifier makes the Picker take up the entire available width in a toolbar. In iOS 17, it tightly fits the width of the text. I can use .fixedSize() modifier to make the segmented control tightly fit the content. In reverse, what is the recommended way to make it expand to fill the whole width? In iOS 17 beta, I tried .frame(idealWidth: UIScreen.main.bounds.width) and it works, but it doesn't have effect in iOS 16 and prior. Also, it is semantically wrong to set the ideal width to an arbitrary big value. Example: iOS 16, full iOS 17, tight
Replies
3
Boosts
0
Views
2.1k
Activity
Aug ’23
IOS UIkit ViewDidLayout Calls On Handle Drag
I am a new uikit developer. I have a problem on my app. App has 2 different parts. Rectangle view and button. Rectangle view can drag with pan touch gesture. Button can change own image with tap gestures. When user taps the button, it changes own image. But when I tap the button, rectangle view moves to the at the beginning position.(view did load position). I checked logs and I see viewDidLayout methods calling when I tap the button. How can I handle this problem, is there anyone help me ? Thanks a lot. import UIKit final class SampleViewController: UIViewController { private var isTapped: Bool = false let button: UIButton = { let button = UIButton() let image = UIImageView(image: UIImage(systemName: "play")) button.translatesAutoresizingMaskIntoConstraints = false button.backgroundColor = .yellow return button }() let littleView: UIView = { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.layer.borderColor = UIColor.red.cgColor view.layer.borderWidth = 4 view.isUserInteractionEnabled = true view.backgroundColor = .red return view }() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(button) self.view.addSubview(littleView) littleView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))) button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) NSLayoutConstraint.activate([ button.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), littleView.heightAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/5), littleView.widthAnchor.constraint(equalTo: self.view.heightAnchor, multiplier: 1/3), ]) } override func viewDidLayoutSubviews() { littleView.frame = CGRect(x: 0, y: 0, width: littleView.frame.width, height: littleView.frame.height) } override func viewWillLayoutSubviews() { littleView.frame = CGRect(x: 0, y: 0, width: littleView.frame.width, height: littleView.frame.height) } @objc private func buttonTapped() { button.setImage(UIImage(systemName: isTapped ? "play.slash" : "play"), for: .normal) self.view.layoutIfNeeded() isTapped.toggle() print("Button tapped") } @objc private func handlePan(_ gesture: UIPanGestureRecognizer) { guard let viewToMove = gesture.view else { return } if gesture.state == .began || gesture.state == .changed { let translation = gesture.translation(in: view) let newX = max(0, min(view.frame.width - viewToMove.frame.width, viewToMove.frame.origin.x + translation.x)) let newY = max(0, min((view.frame.height - viewToMove.frame.height), viewToMove.frame.origin.y + translation.y)) viewToMove.frame.origin = CGPoint(x: newX, y: newY) gesture.setTranslation(CGPoint.zero, in: view) } } } Github link
Replies
1
Boosts
0
Views
805
Activity
Aug ’23
App crashing randomly - Not possible to remove variable
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Not possible to remove variable: 400: unknown var (bug!) with engine as delegate:0x283c8ed40{id: 33112} colIndex:32 from engine NSISEngine: 0x129248130{ delegate:0x12934eaa0 EngineVars:  0: objective{id: 33102} rowIndex:0  1: UIImageView:0x12924ec90.Height{id: 32592} rowIndex:1073741824  2: 0x280a04be0.marker{id: 32591} colIndex:1073741825  3: UIImageView:0x12924ec90.Width{id: 32807} rowIndex:1  4: 0x280dc35a0.posErrorMarker{id: 32871} colIndex:1  5: 0x280dc35a0.negError{id: 32872} rowIndex:88  6: 0x280dc3d80.posErrorMarker{id: 32873} colIndex:1073741826  7: 0x280dc3d80.negError{id: 32874} rowIndex:1073741825  8: UIImageView:0x129353f00.Width{id: 32594} rowIndex:2  9: 0x280979d60.marker{id: 32593} colIndex:3 10: 0x280dc1e60.posErrorMarker{id: 32875} colIndex:4 11: 0x280dc1e60.negError{id: 32876} rowIndex:3 12: UIImageView:0x129353f00.Height{id: 32822} rowIndex:1073741826 13: 0x280dc39c0.posErrorMarker{id: 32877} colIndex:1073741827 14: 0x280dc39c0.negError{id: 32878} rowIndex:1073741898 15: UIButton:0x1293540d0.Width{id: 32817} rowIndex:4 16: 0x280dc2040.posErrorMarker{id: 32879} rowIndex:93 17: 0x280dc2040.negError{id: 32880} colIndex:6 18: UIButton:0x1293540d0.Height{id: 32820} rowIndex:1073741827 19: 0x280dc3de0.posErrorMarker{id: 32881} rowIndex:96 20: 0x280dc3de0.negError{id: 32882} colIndex:1073741830 21: UILabel:0x129353300.Width{id: 32811} rowIndex:5 22: 0x280dc3e40.posErrorMarker{id: 32883} colIndex:44 23: 0x280dc3e40.negError{id: 32884} rowIndex:90 24: UILabel:0x129353300.Height{id: 32842} colIndex:1073741828 25: 0x280dc27c0.posErrorMarker{id: 32885} colIndex:1073741831 26: 0x280dc27c0.negError{id: 32886} rowIndex:1073741828 27: UILabel:0x129353c90.Width{id: 32840} rowIndex:6 28: 0x280dc1da0.posErrorMarker{id: 32887} colIndex:9 29: 0x280dc1da0.negError{id: 32888} rowIndex:97 30: UILabel:0x129353c90.Height{id: 32829} colIndex:1073741832 31: 0x280dc31e0.posErrorMarker{id: 32889} colIndex:1073741833 32: 0x280dc31e0.negError{id: 32890} rowIndex:1073741900 33: UIImageView:0x12934e490.Width{id: 32596} rowIndex:1073741905 34: UIImageView:0x12934e490.Height{id: 32597} colIndex:1073741844 35: 0x280963a70.marker{id: 32595} colIndex:11 36: 0x280dc2ee0.posErrorMarker{id: 32891} colIndex:12 37: 0x280dc2ee0.negError{id: 32892} rowIndex:8 38: 0x280dc1d40.posErrorMarker{id: 32893} colIndex:1073741835 39: 0x280dc1d40.negError{id: 32894} rowIndex:1073741830 40: UIButton:0x1293543b0.Width{id: 32832} rowIndex:9 41: 0x280dc1440.posErrorMarker{id: 32895} colIndex:0 42: 0x280dc1440.negError{id: 32896} rowIndex:1073741845 43: UIButton:0x1293543b0.Height{id: 32827} rowIndex:1073741831 44: 0x280dc3840.posErrorMarker{id: 32897} colIndex:1073741836 45: 0x280dc3840.negError{id: 32898} rowIndex:1073741829 46: UILabel:0x129353a20.Width{id: 32848} rowIndex:10 and another thousand or so lines. this crash occurs after I change the language and open the workout page to start a workout immediately, and sometimes on the second time, I open the workout page.
Replies
10
Boosts
0
Views
7.6k
Activity
Jul ’23
Constrain view's top anchor to just at the edge of sensor housing
What might be a good way to constrain a view's top anchor to be just at the edge of a device's Face ID sensor housing if it has one? This view is a product photo that would be clipped too much if it ignored the top safe area inset, but if it was positioned relative to the top safe area margin this wouldn't be ideal either because of the slight gap between the sensor housing and the view (the view is a photo of pants cropped at the waist). What might be a good approach here?
Replies
0
Boosts
0
Views
1.1k
Activity
Jul ’23
I have facing strange issue related to design in my app.
I have facing issue related design. It's work fine when I run from Xcode and then killed the app and reopened app it breaks the design in some screens. Here, I have attached a video link for your reference. https://drive.google.com/file/d/1-bZMA59UyrquPB5l1C6DkC9XLZSc_zgP/view?usp=share_link
Replies
0
Boosts
0
Views
623
Activity
Jun ’23
App crashing for users, coreautolayout issue with error negativeErrorVarForBrokenConstraintWithMarker
Hi All, Our crash reporting solution show app crashes for the user with crash reports which are not getting symbolicating to anything useful. it just says, App crashed on thread 0 (CoreAutoLayout -[NSISEngine negativeErrorVarForBrokenConstraintWithMarker:errorVar:] with it ending up on main.swift file. We have not able to replicate at our end, and it is happening only for a small fraction of our user (&lt; 1%). This issue is spread across multiple iOS versions and device models. We have verified all our UI actions are being done on main thread. Any pointers for solving this are welcome. Regards, Ishan
Replies
0
Boosts
2
Views
1.3k
Activity
Jun ’23
Stage manager safe area and hidden status bar: How to avoid overlapping by stage manager controls?
Stage manager controls are overlapping top area of an app if the status bar is hidden despite constraints are relative to the safe area. It is surprise for me that the stage manager is not included in the safe area and designed that the status bar will be responsible for making enough margin to avoid overlapping. Is there anything I am missing on how to treat such problem except avoiding controls in the top area at all and enabling the status bar in the app (users got used to it and we don't see it is possible to explain such changes to all of them)? I see that safe are is disrespected, and I do not find any helpful api or settings in interface builder. The only patch I have right now is to auto hide home indicator override func viewDidLoad() { super.viewDidLoad() setNeedsStatusBarAppearanceUpdate() } override var prefersHomeIndicatorAutoHidden: Bool { return true } The stage manager controls are hidden with home indicator until first user interaction. This might help just a little to have at least possibility click something in the top center once in a while. But overall experience is bad. Steps: Create a new project for iOS in Xcode using storyboard as interface. Add a button or any other view/control to the main view controller view in interface builder. Add the constraints to connect the button with its parent viewcontroller’s view: the top constraint should be related to top safe area with 0 space; centerX of the button aligned with parent’s centerX. In main view controller override prefersStatusBarHidden to return true. override var prefersStatusBarHidden: Bool { get { return true } Launch the app and try to click created button. Expected: Created button is clickable and placed slightly below the stage manger’s controls. Safe area does respect the stage manager controls even if the status bar is hidden the same way it behaves in case of avoiding iPhone notches. Or at least there is any API to check if the stage manager is enabled. Actual: Created button is overlapped by the stage manager’s controls and not clickable.
Replies
0
Boosts
1
Views
1.1k
Activity
May ’23
UIViewPropertyAnimator: "Fast" timing parameters and/or low animation duration causes scrubbing to snap between values at the start
I've been playing around with the awesome UIViewPropertyAnimator, but are observing something during scrubbing I don't quite understand. Linearly scrubbing a UIViewPropertyAnimator configured with "fast" timing parameters and/or low animation duration, causes the animated properties to snap between (larger) values at the start of scrubbing. This can be made visible with a simple bottom sheet example (recorded on iPhone 14 Pro with iOS 16.4.1): Small/"fast" timing parameters: Large/"slow" timing parameters: The UIViewPropertyAnimator is instantiated as follows (the issue also observed with other initializers): // duration set to 0.25 gives the unwanted snapping at the start of scrubbing: UIViewPropertyAnimator(duration: 0.25, dampingRatio: 1) // duration set to 1.25 gives the expected smooth scrubbing. UIViewPropertyAnimator(duration: 1.25, dampingRatio: 1) Any ideas about what's going on here? I would expect the scrubbing to be smooth independent of what parameters are given? It's especially odd that the snapping only happens at the start of the scrubbing... Also attached the full example code: BottomSheetAnimationViewController.txt
Replies
0
Boosts
0
Views
1.3k
Activity
May ’23
Problem with NSTableView constraints
Hi, the problem is not dangerous to life, I don't want see the yellow button on top of the editor. 'Autoresizing Mask' is set to about 50 items, that works, except tableView, 10 warnings. The tableView has 10 columns of 20 pixels width, min 10 and max 100000. The ' Add new constraints' options are all grayed, so what ? In an other project a warning was set for arrayController, I don't like Auto Layout, the blue help lines are enough. My question is : How do i can suppress this warnings ? Kindly, Uwe
Replies
0
Boosts
0
Views
1.1k
Activity
May ’23
How does NSStackView effect NSTableView.bounds rowAtPoint and NSEvent.locationInWindow
I'm trying to get the row index of a NSTableRowView that has experienced a mouseEnter event. It should be easy with rowAtPoint using NSEvent.locationInWindow. I can't get it to work. The NSTableView lies inside a NSStackView. Of course the NSStackView adjusts the visual size of the NSTableView etc. Is NSStackView interfering? If so, how should I adjust my call to rowAtPoint?
Replies
0
Boosts
0
Views
844
Activity
Apr ’23
Notification dot for camera and mic doesn’t stay inside the Dynamic island
hello Developers, im using Iphone 14 pro max and found out that when the Device camera or mic is in use then a notification dot can be seen in the Dynamic island. But if the notification dot is there and we perform another action related to Dynamic island. Like set a Timer or Connect the device with charger. Then the notication dot from middle goes all the way to the right side of the screen. Doesn’t it should just stay in the middle? As none of the other dynamic island related actions consumes that dead pixel part of screen.
Replies
2
Boosts
0
Views
854
Activity
Apr ’23
Adding and positioning views to UITableViewCell.contentView
Hi there, the documentation explicitly says this is what's contentView for. I've just tried and found big problems not adding — works like a charm — but positioning my view so that it does not clash with the standard ones. I've added a small badge at the rightmost edge of contentView. Having added the subview into contentView (in tableView:cellForRowAtIndexPath:, after checking that it does not exist yet in a reused cell view), I set up constraints so that it is Y-centered and sticks to the right edge of the contentView, that was very easy and worked nicely at the first try: UILayoutGuide *lg=cell.contentView.layoutMarginsGuide; [badge.trailingAnchor constraintEqualToAnchor:lg.trailingAnchor].active=YES; [badge.centerYAnchor constraintEqualToAnchor:lg.centerYAnchor].active=YES; I've found though if the text in textLabel (I have to address iOS12+, thus I can't use content configurations) is long enough, it clashes with the badge instead of clipping/wrapping/whatever it is set to (wrapping in my case). Thus I've added another constraint which makes sure the trailing edge of textLabel does not exceed the leading edge of my badge: UILayoutGuide *blg=badge.layoutMarginsGuide; [cell.textLabel.trailingAnchor constraintLessThanOrEqualToAnchor:blg.leadingAnchor constant:-6].active=YES; Oops: that pushed the badge right, so that it overlaps the accessory area. By my further testing, seems the culprit is another constraint (probably created dynamically later from the autoresizing mask) which bounds the right edge of textLabel to the right edge of contentView. I've tried to find and remove (deactivate) it in tableView:cellForRowAtIndexPath:, in vain, well, makes some sense perhaps, too soon. I've tried to find and remove it in tableView:willDisplayCell:forRowAtIndexPath:; no luck either. To me, this does not make sense at all. (It sort of looks like the documentation claim “After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out” is highly overstated. Self-evidently, non-trivial layout stuff happens after the delegate returns, but before the cell is drawn.) Eventually, having used updateConstraints explicitly (which does not feel right), I succeeded to find the darn thing, a fixed width constraint for textLabel. Tried to deactivate it: does not work, setting its active property to NO does nothing, no warning, no error, no exception, thing just stays at YES. I've tried to decrease its priority, that's even more funny: it goes down, when checked, it is down, but 1000 is somehow re-surrected after tableView:willDisplayCell:forRowAtIndexPath: returns, but before the cell is finally laid out and drawn. Ick. What's the proper solution of this? I suppose it should be pretty easy, but it is not, or I must be overlooking something pretty obvious. Thanks!
Replies
0
Boosts
0
Views
1.1k
Activity
Mar ’23
Macos SwiftUI App window size issue
Hi, I am new to MacOS SwiftUI Development. I am creating Simple Login Screen with fixed width and height which it is showing differently in different screen resolutions. Please suggest designing the screen suitable to any screen resolution.
Replies
0
Boosts
0
Views
1.1k
Activity
Mar ’23
NSISEngine Auto Layout Crash (Not possible to remove variable)
Hey there, We're experiencing a big bump of these crashes with no great explanation. We're not doing anything special about the layout there except for using some nested Stack Views, and the view itself is embedded within a reusable cell. Attaching a full crash log - have plenty more of these but they are quite similar. Would appreciate your help :) Shai. 2021-10-20_11-48-29.1535_+0300-c0ce73e85344ddcdc178995f3e83de0b406bab54.crash
Replies
4
Boosts
2
Views
3.6k
Activity
Mar ’23
Interface Builder changes x, y, width and height values randomly.
I have old projects without autolayout. When I open the storyboard in Interface Builder everything looks fine. But if I move a button or label by one pixel, for example, or add a new UIView, then the Interface Builder randomly changes the x, y, width, or height values for almost all elements in all UIViewControllers in the storyboard file. Not all values are changed, but a lot. If I close and open Storyboard a few times and do an action, the values are changed so many times that the user interface is completely broken. Does anyone have an idea how I can fix the problem? I am using Xcode 14.0.1 and it happens with all projects I have. The changes look like this, for example:
Replies
10
Boosts
1
Views
3.1k
Activity
Feb ’23
Precompute the height of a UITableView with dynamically sized cells
Howdy, I have a UITableView which is embedded deeply (i.e. nested in other views) within another UIScrollView for reasons. It seems that in order to get this outer UIScrollView to scroll properly, I have to constrain the height of this UITableView ahead of time. My problem is that I can't figure out how to calculate this height quickly with dynamically sized cells (thus contentSize and related methods won't work). The best method I have been able to come up with is to scroll to each cell individually and fetch their height ahead of time sometime during/after layout, which is less than ideal... I imagine I could also adjust the height as cells are being loaded, but that doesn't sound like good user experience (scroll view keeps growing, wouldn't it be choppy?). On quick investigation, it seems that a regular (i.e. non-embedded) UITableView is able to get an accurate height of dynamically sized cells ahead of time (as seen by the scroll bar). I'm curious if this means that there is something I am missing? I'm sure there is a lot left for me to learn. I will appreciate any help. Thanks, smkuehnhold
Replies
1
Boosts
0
Views
2.3k
Activity
Jan ’23
Got crashed in my App
got warnings as below every time I perform segue: [Assert] UINavigationBar decoded as unlocked for UINavigationController, or navigationBar delegate set up incorrectly. Inconsistent configuration may cause problems. navigationController=<UINavigationController: 0x10bf01280>, navigationBar=<UINavigationBar: 0x10bab7c80; frame = (0 0; 0 50); opaque = NO; autoresize = W; layer = <CALayer: 0x118e83f80>> delegate=0x10bf01280 Seems it does not direct crash my App directly since I always got this warning after a new iOS (forgot which version). But I got a crash as the attached report, not sure any relation with this warning, or due to another cause, thank you for your help. 2023-01-12, 05-24.crash
Replies
0
Boosts
0
Views
1.4k
Activity
Jan ’23
A crash triggered by backgroud thread which is hard to catch, seems UI main thread updating issue.
2022-11-10, 17-40.crash
Replies
6
Boosts
1
Views
4.6k
Activity
Jan ’23
Safe area guide top anchor lost after force change orientation
Hi, I wrote a simple auto layout constraint below and used a force change orientation method to present a full-screen landscape view controller. imageView.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor, constant: 16) After the landscape view controller is dismissed, the safe area guide seems broken, the top safe area insets change to zero, and call layoutIfNeeded() won't fix this issue. The only way to fix this is back to Home Screen and reopen the app. This problem occurred on the iPhone SE simulator, newer devices with a notch don't have this issue. Does anybody know how to fix this? func forceOrientation(orientation: UIInterfaceOrientation) {     let value = orientation.rawValue     if #available(iOSApplicationExtension 16.0, iOS 16.0, *) {         let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene         var interfaceOrientations: UIInterfaceOrientationMask = []         switch orientation {         case .portrait:             interfaceOrientations = .portrait         case .landscapeLeft:             interfaceOrientations = .landscapeLeft         case .landscapeRight:             interfaceOrientations = .landscapeRight         case .portraitUpsideDown:             interfaceOrientations = .portraitUpsideDown         case .unknown:             interfaceOrientations = .portrait         @unknown default:             interfaceOrientations = .portrait         }         windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: interfaceOrientations))     } else {         UIDevice.current.setValue(value, forKey: "orientation")         UINavigationController.attemptRotationToDeviceOrientation()     } }
Replies
0
Boosts
0
Views
1.5k
Activity
Jan ’23
Enable Inspector in WKWebView under SwiftUI & SplitView
I am running this on MacOS, Xcode version 11.6. In order to run this code you must add com.apple.security.network.client with the value true to your entitlements file. I want to enable the Inspector for WKWebView. I have created an NSViewRepresentable to encapsulate the WKWebView. Here is the simplified code: struct WKWebViewRepresentable: NSViewRepresentable { &#9;&#9;var urlToDisplay: URL? &#9;&#9;var enableDeveloperTools: Bool = false &#9;&#9;func loadData(wkWebView: WKWebView) { &#9;&#9;&#9;&#9;if let url = urlToDisplay { &#9;&#9;&#9;&#9;&#9;&#9;wkWebView.load(URLRequest(url: url)) &#9;&#9;&#9;&#9;} &#9;&#9;} &#9;&#9;func makeNSView(context: Context) -> WKWebView { &#9;&#9;&#9;&#9;let wkWebView = WKWebView() &#9;&#9;&#9;&#9;wkWebView.configuration.preferences.setValue(enableDeveloperTools, forKey: "developerExtrasEnabled") &#9;&#9;&#9;&#9;loadData(wkWebView: wkWebView) &#9;&#9;&#9;&#9;return wkWebView &#9;&#9;} &#9;&#9;func updateNSView(_ wkWebView: WKWebView, context: Context) { &#9;&#9;&#9;&#9;loadData(wkWebView: wkWebView) &#9;&#9;} &#9;&#9;func makeCoordinator() -> Coordinator { &#9;&#9;&#9;&#9;return Coordinator(self) &#9;&#9;} } extension WKWebViewRepresentable { &#9;&#9;class Coordinator: NSObject { &#9;&#9;&#9;&#9;init(_ parent: WKWebViewRepresentable) { &#9;&#9;&#9;&#9;&#9;&#9;super.init() &#9;&#9;&#9;&#9;} &#9;&#9;} } and with this code in place I can create the following ContentView: struct ContentView: View { &#9;&#9;static let demoURL = "https://" + "www.apple.com/apple-music/" &#9;&#9;var body: some View { &#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;WKWebViewRepresentable(urlToDisplay: URL(string: ContentView.demoURL), enableDeveloperTools: true) &#9;&#9;&#9;&#9;&#9;&#9;WKWebViewRepresentable(urlToDisplay: URL(string: ContentView.demoURL), enableDeveloperTools: false) &#9;&#9;&#9;&#9;} &#9;&#9;} } The good news is that the above code actually works. If you right click on the left hand side, you can select Inspect Element which will start the Inspector. The idea is that I would do discovery on the left hand side and do show the processed result on the right hand side. That part is obviously not in the code. For testing purposes the right hand side simply shows the same content as the left hand side. I would like to see that the user is being able to split the real-estate between the two views, hence I replace the HStack with HSplitView, no other change. When you run this code, the WkWebView's come up as before but one you select Inspect Element the place where the Inspector should show is blank (black or white depending on dark mode), it starts flashing etc. By dragging inside that rectangle, it becomes clear that the code is actually running just not properly rendering. This stackoverflow - https://stackoverflow.com/questions/60727065/wkwebview-web-inspector-in-macos-app-fails-to-render-and-flickers-flashes article, mentions the same problem and it suggests that autolayout should be disabled. Does anyone know a trick to circumvent this layout problem for SwiftUI?
Replies
1
Boosts
0
Views
4.6k
Activity
Dec ’22