How to move a view based on validation uikit swift - uikit

I have a validation inside of my app and a textfield both are located on different areas of the same screen.
Textfield is on y = 200
Validation Button is on y = 800
Whenever I am at the point of the validating of y = 800 and my textfield is empty I need to automatically scroll on the background to y which is 200 from y = 800.
In short
if textfield is empty I need to go to this specific place (y=200) which is located in the beginning of the app.
I am not sure how to do it in uikit.

If you are using UIScrollView, you can call scrollRectToVisible and pass the textField's frame as parameter to this function.
scrollView.scrollRectToVisible(textField.frame, animated: true)

Related

SwiftUI UI Test How To Display Dynamic Buttons Text Values in an app.scrollViews?

How To Display Dynamic Buttons Text Values in an app.scrollViews?
I would like to able to tap the button inside first row in the scrollViews, but not sure what the index of the button is. I tried the 1, 2 and 3 with no luck.
let scrollViewsQuery = app/*#START_MENU_TOKEN#*/.scrollViews/*[[".otherElements[\"Tabbar\"].scrollViews",".scrollViews"],[[[-1,1],[-1,0]]],[0]]#END_MENU_TOKEN#*/
let elementsQuery = scrollViewsQuery.otherElements
elementsQuery.buttons.element(boundBy: 0).tap() //
print("----------------------------------------------")
var i = 0
for element in elementsQuery.buttons.allElementsBoundByIndex{
i += 1
print(i)
print(element) //How To Display the Button Text here?
// print( elementsQuery.buttons.element(boundBy: i))
}
Assuming you only have one scrollView present, the code to tap the first button in it would be the following:
let myScrollView = app.scrollViews.firstMatch
let myScrollViewsButtons = myScrollView.buttons
let myScrollViewsFirstButton = myScrollViewButtons.firstMatch
myScrollViewsFirstButton.tap()
A button in this context is an XCUIElement, not something that is particularly printable. Buttons do have label attributes that are generally the text displayed on them...

XCUIElement exists, but is not hittable

In my UI tests, I create a UIView programmatically using (shortened)
let topMarker = UIView.init(frame: CGRect.init())
…
topMarker.accessibilityIdentifier = kTopMarker
The topMarker view is in a custom table view cell.
In my UI tests, I use
let new1stCell = app.cells.element(boundBy: 0)
let topMarker = new1stCell.otherElements[kTopMarker]
let topMarkerExists = topMarker.waitForExistence(timeout: 15)
XCTAssertTrue(topMarkerExists, "Top marker does not exist")
XCTAssertTrue(topMarker.isHittable, "Top marker is not hittable")
When I set a Test Failure breakpoint, the test stops at the last line, i.e. topMarker exists, but is not hittable.
On the other hand, I can see the view in the snapshot, i.e. it exists and is visible.
This is stange, because the docs say:
isHittable returns true if the element exists and can be clicked, tapped, or pressed at its current location. It returns false if the element does not exist, is offscreen, or is covered by another element.
I thought, maybe it is visible, but cannot be clicked, tapped, or pressed, because userInteractionEnable is not true, but even if I set this property to true, the view does not become hittable.
What am I missing?
Problem solved:
An XCUIElement is only hittable, if its isAccessibilityElement property is set to true.
The docs to property isAccessibilityElement say
The default value for this property is false unless the receiver is a standard UIKit control, in which case the value is true.
Assistive applications can get information only about objects that are represented by accessibility elements. Therefore, if you implement a custom control or view that should be accessible to users with disabilities, set this property to true.
My UIView that I instantiated programmatically is not a standard UIKit control. As soon as I added
topMarker.isAccessibilityElement = true
the test
XCTAssertTrue(topMarker.isHittable, "Top marker is not hittable")
succeeded.

Getting page dimensions

On navigating to a new page, how do I get the dimensions of the page - the client/layout area only - exclude the action bar if present.
Will I get the same notification when the device orientation changes?
Thanks.
Every view in NativeScript has a method call getMeasuredHeight() as well as getMeasuredWidth(). You can get the page dimensions like this:
var pageHeight, pageWidth;
pageHeight = page.getMeasuredHeight()- page.actionBar.getMeasuredHeight();
pageWidth = page.getMeasuredWidth()- page.actionBar.getMeasuredWidth();

UITextField not scrolling horizontally

I am trying to make a small calculator app.
When a UIButton is pressed, the Button title is added to a UITextField.
kind of:
myuitextfield.text = [myuitextfield.text stringByAppendingString:[button currentTitle];
When I reach the end of my textfield, the text gets truncated. How can I disable this, so the textfield starts scrolling automatically and allows adding more characters?
I tried every possible option in Interface Builder, without any luck.
Isn't the UITextField supposed to scroll automatically? I can see this behavior when a native keyboard is used and text is entered.
I have chosen UITextField, as I need only 1 Line.
To illustrate the Problem:
When I enter text using my custom UIButtons text gets truncated
When I tap the UITextField and enter text using the keyboard I can enter unlimited text and the text is not truncated.
If you are facing this issue on iOS7, I've managed to fix it after been inspired by this post. In my case I had a field for entering an email address and after reaching the edge, the user could carry on typing but the text would be invisible (off-field).
First, add a callback to your UITextField so that you can track a text change to the field:
[self.field addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
Then evaluate the size in pixels of the entered string as it is typed and change the text alignment from left to right when reaching the edge of the field area:
- (void)textFieldDidChange:(NSNotification *)aNotif{
float maxNumPixelsOnScreen = 235; // Change this value to fit your case
CGSize maximumSize = CGSizeMake(maxNumPixelsOnScreen + 10, 1);
NSString *aString = self.field.text;
CGSize stringSize = [aString sizeWithFont:fieldFont
constrainedToSize:maximumSize
lineBreakMode:NSLineBreakByWordWrapping];
self.field.textAlignment = NSTextAlignmentLeft;
if (stringSize.width >= maxNumPixelsOnScreen)
self.field.textAlignment = NSTextAlignmentRight;
}
Note:
self.field is the offending UITextField
maximumSize: I'm adding 10 the the width to be slightly over the limit defined
fieldFont is the UIFont used to render the text field
Hope it helps!
you have to add UITextview and limit the number of lines to 2.Textfield doesnt work with two lines.Textview is same as textfields except the delegates and some properties differ.

expanding a text view when user clicks on "more..."

There are applications with a text field that shows a given amount of lines and when the user clicks more it expands and shows the rest,
I'm familiar with the code to expand my UITextView but not sure how to add the "more..." and then "less...",
option to
Fine tuning the question: I'm not looking to add a button but having the default IOS behavior when the text is larger than the size of a control it will either place ... at the end or in the middle for example: this is ... or thi...long
you can take one button and add that UIButton to view below the UITextView and when you click on that button change the button positions and title related to the UITextView.
You can implement that using the Method (sizeWithFont:constrainedToSize:lineBreakMode:)
Firstly take out the size of the text you need show into the TextField like this
NSString *textToBeShown = #"jhfsdg djsgf jdsfsdf dsf";
CGSize textSize = [textToBeShown sizeWithFont:[UIFont fontWithName:#"Arial-BoldMT" size:11]]
constrainedToSize:CGSizeMake(constrainedWidth,constrainedHeight)
lineBreakMode:NSLineBreakByWordWrapping];
Suppose you want your textview height to be 100.0 initially before pressing "ShowMore" button then you can check the textSize.height in comparison to the 100.0 , if the textHeight is less then 100.0 then assign the TextView's Heigth as textSize.height else assign it 100.0 pixel .
Now when the show more button is clicked assign the textSize.height as the height of the UITextView used to show the text .
NSString *textToBeShown = #"jhfsdg djsgf jdsfsdf dsf";
CGSize textSize = [textToBeShown sizeWithFont:[UIFont fontWithName:#"Arial-BoldMT" size:11]]
constrainedToSize:CGSizeMake(constrainedWidth,constrainedHeight)
lineBreakMode:NSLineBreakByWordWrapping];
textView.frame=CGRectMale(textView.frame.origin.x,
textView.frame.origin.y,
textSize.width,
textSize.height);

Resources