swift playground UITextField spawns keyboard that is too big - keyboard

Swift in playground on Mac OS. When the user clicks in a UItextfield, a keyboard spawns but it is very large compared to the view and only the first few keys are available.
minimal example:
import UIKit
import PlaygroundSupport
class TesterViewController : UIViewController {
var testTextField : UITextField!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
testTextField = UITextField()
testTextField.borderStyle = .roundedRect
testTextField.text = ""
view.addSubview(testTextField)
testTextField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
testTextField.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
testTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
])
self.view = view
}
}
PlaygroundPage.current.liveView = TesterViewController()
screenshot

I face the same issue. It seems as if Playground has a hard-coded screen size of 768x1024 (run UIScreen.main.bounds in the Playground) and shows the keyboard according to this size, independently of the live view's actual size.
The best workaround I came up with is to increase the size of the view controller so that it matches the keyboard:
let vc = TesterViewController()
vc.preferredContentSize = vc.view.frame.size // or a custom CGSize
PlaygroundPage.current.liveView = vc
Of course this makes the view larger than you might want it to be, so I only use this workaround when I really have to access the on-screen keyboard for testing.

System keyboard is presented according to the key window size and in the case of a playground simulator the size is 768x1024. It looks like it is a bug.
A solution which works:
Instead of passing a view controller one should pass a window with a custom size.
let window = UIWindow(frame: CGRect(x: 0,
y: 0,
width: 768,
height: 1024))
let viewController = MyViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()
PlaygroundPage.current.liveView = window
Works in Xcode 11 and above (latest tested version Xcode 12.2)
Edited
I have checked the answer which suggests to use "vc.preferredContentSize" but it doesn't work for me. Tested in Xcode 12.2
let vc = TextFieldViewController()
vc.preferredContentSize = CGSize(width: 768,height: 1024)
PlaygroundPage.current.liveView = vc

In reply to Adobels (Seo 21 '19) ...
I am not sure why he proposed a different approach.
I modified dr_barlo's approach of Oct 22 '17 as follows (and it works in Xcode 11 too):
let vc = TextFieldViewController()
vc.preferredContentSize = CGSize.init(width: 768,height: 1024)
PlaygroundPage.current.liveView = vc
They all appear equivalent in results if not internal workings.

Related

SwiftUI Text justified alignment

Has anyone found a way to align the string in a Text in a justified way?
In my reading app I'd like to offer the user to choose the justified alignment, where both the .leading both the .trailing alignments are true at the same time. Unfortunately the .multilineTextAlignment modifier for the Text has only three options: .leading, .center and .trailing.
If not I can still wrap an UILabel into a UIViewRepresentable since the UILabel has the .justified alignment option, but I thought it should be available in the Text as well and I was not able to find it.
I know I'm late but I faced same issue , I will write it here, it could help other developer in future
Unfortunately until now Apple doesn't suppourt .justified in SwiftUI 2.0
you need to depend on UIKit
Add this code in your project
struct LabelAlignment: UIViewRepresentable {
var text: String
var textAlignmentStyle : TextAlignmentStyle
var width: CGFloat
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.textAlignment = NSTextAlignment(rawValue: textAlignmentStyle.rawValue)!
label.numberOfLines = 0
label.preferredMaxLayoutWidth = width
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentHuggingPriority(.required, for: .vertical)
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = text
}
}
enum TextAlignmentStyle : Int{
case left = 0 ,center = 1 , right = 2 ,justified = 3 ,natural = 4
}
Then use it like this
LabelAlignment(text: "Your text here", textAlignmentStyle: .justified, width: UIScreen.main.bounds.width - 20)
change 20 to any number you like to add space on left and right
my solution will will depend on text content size
if the text is small the view will be small and vice versa
so it will act as Text()

Remove UIRefreshControl default spinner

I have subclassed UIRefreshControl to make my own, and I can manage everything except one thing :
The default spinner is always visible, and is of course in the middle of everything I've done in my custom implementation.
I can't find the spinner property or whatever it is I need to put to a clear color, can anyone help me on this?
To hide spinner loader in a UIRefreshControl, set the .tintColor property to a clear color.
e.g. (Swift):
mRefreshControl.tintColor =.clear
For 2023, .tintColor = .clear works fine.
Historic answer:
Swift 4, Swift 5
Bug fix
From time to time only this code doesn't work properly:
refreshControl.tintColor = .clear – first reload shows the indicator :(
This fixes this bug:
refreshControl.tintColor = .clear
refreshControl.subviews.first?.alpha = 0
Override didMoveToSuperview to hide the spinner's superview.
Swift version:
override func didMoveToSuperview() {
super.didMoveToSuperview()
guard let _ = superview else { return }
self.subviews.first?.alpha = 0 // set hidden = true did not work
}
Objective-C version:
- (void)didMoveToSuperview {
[super didMoveToSuperview];
if (self.superview != nil && self.subviews.count > 0) {
self.subviews[0].alpha = 0;
}
}
You may want to do more checks rather than just using the first subview.

Positioning sprites inside scene

I would like when i create all my sprites to get centred in the screen, but that is not the case, i have a sprite when i set it's position to CGPointMake(self.size.width / 2, self.size.height / 2) it gets centred vertically but not horizontally, when i set it to CGPointMake(0, 0) it sets at the bottom of the screen with only half of it's body visible ( which is what you expect) but horizontally it doesn't get positioned right ( half his vertical body appears ) until i set it's X to 300.
How come the sprite's Y position gets set right but the X doesn't ?
scene.scaleMode = .AspectFill
and i haven't set any anchorPoint, 4' screen.
UPDATE:
I'm using Portrait, played around with different scaleMode and found out that the perfect (0, 0) that sets the sprite on the bottom left is:
scene.scaleMode = .ResizeFill
It also works fine with different screen sizes,but (self.size.width / 2) puts it further to the left.Do you think it's the best setting for all SpriteKit Portrait projects?
I tried to reproduce what you are saying, but I couldn't, either using .sks file or not for loading the scene. Also I've produced good results on both 7.1 and 8.1 simulators...The thing is that simulators sometimes could be tricky and not 100% reliable, so you should not trust them much.
I don't know if this will help you, but you should consider it...In Xcode 5 projects scene is programmatically created to have a size of the view. Basically it's done in viewDidLoad method like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Create and configure the scene.
GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
Or using viewWillLayoutSubviews like LearnCocos2D pointed:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
skView.showsDrawCount = YES;
//skView.showsQuadCount = YES;
skView.showsPhysics = YES;
skView.ignoresSiblingOrder = YES;
if(!skView.scene){
// Create and configure the scene.
GameScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
But Xcode 6 uses an SKS file:
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
}
}
If you look at the GameScene.sks you can see that the default scene size is 1024x768.
Try to print your scene size as well as view's bounds size to see actuall sizes. You can use something like:
println("view.bounds\(view.bounds), self.size\(self.size)")
I don't know if any of this helped, but I hope it will lead you somewhere. Goodluck!

Custom controls not visible in the View

Below is my code I used to create a custom UITextField in code behind.
UITextField usernameField;
usernameField = new UITextField
{
Placeholder = "Enter your username",
BorderStyle = UITextBorderStyle.RoundedRect,
Frame = new RectangleF(10, 32, 50, 30)
};
View.AddSubview(usernameField);
But when I run my app, I dont see this anywhere. Not only this control but all the controls that I create in code behind.
If I drag the controls from toolbox onto my View it's working very fine.
What might be the cause?
Make sure that Container View is not transparent so Alpha = 1 and set background colors for appropriate controlls. Because by default its set to ClearYou can use this method to easly set background and rounded corners like in older versions of iOS
public static void MakeViewRounded(UIView view,float cornerRadius, MonoTouch.CoreGraphics.CGColor backgroundColor)
{
view.Layer.CornerRadius = cornerRadius;
view.Layer.BackgroundColor = backgroundColor;
}
Also make sure that you adding your custom controll to ViewController/View

NSSplitViewItem collapse animation and window setFrame conflicting

I am trying to make a (new in 10.10) NSSplitViewItem collapse and uncollapse whilst moving its containing window so as to keep the whole thing "in place".
The problem is that I am getting a twitch in the animation (as seen here).
The code where I'm doing the collapsing is this:
func togglePanel(panelID: Int) {
if let splitViewItem = self.splitViewItems[panelID] as? NSSplitViewItem {
// Toggle the collapsed state
NSAnimationContext.runAnimationGroup({ context in
// special case for the left panel
if panelID == 0 {
var windowFrame = self.view.window.frame
let panelWidth = splitViewItem.viewController.view.frame.width
if splitViewItem.collapsed {
windowFrame.origin.x -= panelWidth
windowFrame.size.width += panelWidth
} else {
windowFrame.origin.x += panelWidth
windowFrame.size.width -= panelWidth
}
self.view.window.animator().setFrame(windowFrame, display: true)
}
splitViewItem.animator().collapsed = !splitViewItem.collapsed
}, completionHandler: nil)
}
}
I am aware of the "Don't cross the streams" issue (from session 213, WWDC'13) where a window resizing animation running on the main thread and a core animation collapse animation running on a separate thread interfere with each other. Putting the splitViewItem collapse animation onto the main thread seems like the wrong approach and I've got a nagging feeling there's a much better way of doing this that I'm missing.
Since I am not finding any documentation on the NSSplitViewItems anywhere (yet) I would appreciate any insights on this.
I have the little test project on GitHub here if anyone wants a look.
Update The project mentioned has now been updated with the solution.
Thanks,
Teo
The problem is similar to the "don't cross the streams" issue in that there are two drivers to the animation you've created: (1) the split view item (2) the window, and they're not in sync.
In the example from the '13 Cocoa Animations talk, constraints were setup to result in the correct within-window animation as only the window's frame was animated.
Something similar could be tried here -- only animating the window's frame and not the split view item, but since the item manages the constraints used to (un)collapse, the app can't control exactly how within-window content animates:
Instead the split view item animation could completely drive the animation and use NSWindow's -anchorAttributeForOrientation: to describe how the window's frame is affected.
if let splitViewItem = self.splitViewItems[panelID] as? NSSplitViewItem {
let window = self.view.window
if panelID == 0 {
// The Trailing edge of the window is "anchored", alternatively it could be the Right edge
window.setAnchorAttribute(.Trailing, forOrientation:.Horizontal)
}
splitViewItem.animator().collapsed = !splitViewItem.collapsed
}
For anyone using Objective C and targeting 10.11 El Capitan.
This did the trick for me, didn't need to set AnchorAttributes.
splitViewItem.collapsed = YES;

Resources