Post

Replies

Boosts

Views

Activity

Reply to Using .center to move a subview within its parent
As far as I tried your code, the statement primaryPOIMenu.center.x += 88 works as expected. (The background color of POIMenuViews is set to system yellow.) But many things depend on the settings of your storyboard and xib. What do you get when you comment out the line modifying center?         //primaryPOIMenu.center.x += 88   // This NO WORKY!!!  Why???
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Keeping Track of Text Changes over Two Text Fields
May I ask how I can receive a bool result in my view controller like Generally, you should not include two or more topics in one thread..., though I'm not sure if this would be a part of the first topic or not. I would do that as follows: ViewModel @Published var isUserValid: Bool = false func validateUser(username: String?, password: String?) { if let username = username, !username.isEmpty, let password = password, !password.isEmpty { isUserValid = true } else { isUserValid = false } } ViewController private var cancellables = Set<AnyCancellable>() // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) viewModel.$isUserValid .removeDuplicates() .sink {isUserValid in print(isUserValid) //... } .store(in: &cancellables) }
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to Keeping Track of Text Changes over Two Text Fields
I'm still a beginner in using Combine. Is there a better approach in seeing text changes over two text fields at the same time using Combine? Unfortunately, it is hard to find what would be the best practice when using Combine in UIKit apps, as, you may know, there are no simple ways to integrate Combine and UIKit. So, every developer is a beginner. But one thing critically bad in your example is that you create a new instance of LoginViewModel at each time Notification is received. Other than that, I cannot say which is the better as there are very few examples on the web. The following is just that I would write something like this in cases you described: ViewModel import UIKit import Combine class LoginViewModel { //↓Use _plural_ form var cancellables = Set<AnyCancellable>() init() { $myUsername .removeDuplicates() .combineLatest($myPassword.removeDuplicates()) .sink(receiveValue: {username, password in self.validateUser(username: username, password: password) }) .store(in: &cancellables) } convenience init(username: String, password: String) { self.init() myUsername = username myPassword = password } @Published var myUsername: String? @Published var myPassword: String? func validateUser(username: String?, password: String?) { print("\(username ?? "")") print("\(password ?? "")") //If you need to update some UI, add another publisher to which the ViewController can subscribe to. //... } } import UIKit extension LoginViewModel { func bind(_ textField: UITextField, to property: ReferenceWritableKeyPath<LoginViewModel, String?>) { NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: textField) .sink(receiveValue: { result in if let textField = result.object as? UITextField { self[keyPath: property] = textField.text } }) .store(in: &cancellables) } } ViewController import UIKit import Combine class HomeViewController: UIViewController { // MARK: - Variables let viewModel = LoginViewModel() // MARK: - IBOutlet @IBOutlet var usernameTextField: UITextField! @IBOutlet var passwordTextField: UITextField! // MARK: - Life cycle override func viewDidLoad() { super.viewDidLoad() viewModel.bind(usernameTextField, to: \.myUsername) viewModel.bind(passwordTextField, to: \.myPassword) } }
Topic: UI Frameworks SubTopic: UIKit Tags:
Oct ’21
Reply to xCode 13.1 -- MKMap mapView.addOverlay -- Compiler Error: Invalid library file
If you could show a complete code (including MapViewModel and the view showing MKMap), more readers would be involved solving your issue. The message Compiler Error: Invalid library file seems to be so called a log noise and you may need to ignore them. You can find many articles discussing the issue searching with "Compiler Error: Invalid library file". You can file a bug report, but do not expect too much, this issue has being left for years. Your implementation of UIViewRepresentable has some inappropriate parts, but the core reason you cannot find the overlay on map might be because you have not set colors of MKCircleRenderer. struct MKMap: UIViewRepresentable { @ObservedObject var vm: MapViewModel = MapViewModel.shared //↓Do not instantiate a UIView in a property initializer //let mapView = MKMapView() func makeCoordinator() -> Coordinator { Coordinator(self) } // This funciton is called when we make the view func makeUIView(context: Context) -> MKMapView { let mapView = MKMapView() //<- mapView.delegate = context.coordinator mapView.userLocation.title = "user" mapView.showsUserLocation = true let point = CLLocationCoordinate2D(latitude: -73.68118286132812, longitude: 45.48589125320114) let overlay = MKCircle(center: point, radius: 30) mapView.addOverlay(overlay) let region = MKCoordinateRegion(center: vm.center, span: vm.span) mapView.setRegion(region, animated: true) return mapView } // Function called when the view is updated func updateUIView(_ mapView: MKMapView, context: Context) { //↓↓You need to prepare for updates... let region = MKCoordinateRegion(center: vm.center, span: vm.span) mapView.setRegion(region, animated: true) //... } } class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate{ var parent: MKMap init(_ parent: MKMap){ self.parent = parent } func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { if let circle = overlay as? MKCircle { //<- print("--->>> I'm inside the rendererFor overlay in MKCircle Statement <<<---") let renderer = MKCircleRenderer(circle: circle) //<- renderer.fillColor = .green //<-# renderer.lineWidth = 100 //<- Isn't it too wide? renderer.strokeColor = .red //<-# return renderer } return MKOverlayRenderer() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’21
Reply to What to use for animating app for iOS
Which UI framework are you using? UIKit or SwiftUI? I have no knowledge of Adobe Animate, but you may need to learn and find what would be the iOS way of animating. Learning Core Animation APIs will help you constructing animations, but in most simple cases, using animations in iOS apps is far more simple. You can find many tutorials or guides searching with "ios animation". Or to get more specific results, "ios uikit animation" or "ios swiftui animation". Better try one of them and post more specific issues you experience.
Oct ’21
Reply to Xcode Simulator Rotate
Why isn't the app screen properly rotated when the app first launches in the simulator? I'm not sure if Apple's engineer would respond to this question. But none of the software features cannot move the actual physical device orientation in the hand of user. So, the simulator is properly simulating the behavior of your app, when user holds an iPad in the portrait orientation. In a few generations ago, old Xcode are changing orientation according to the orientation settings of the app. In fact, I was a little confused when this change was first introduced. But, now, I am accustomed to it and thinking that this behavior is quite reasonable.
Oct ’21