EDIT: I updated the question so it is more clear.
Is there a way to check the constraints and get an NSArray of conflicting ones?
I'm working on an Autolayout helper class and I would like to print a pretty log when there are conflicts or errors.
By default, when there are layout conflicts you get a log that shows the conflicting constraints like this:
"<NSLayoutConstraint:0x7fe110638610 UILabel:0x7fe11062bb80'green'.leading == UIView:0x7fe11070de50.leadingMargin>",
"<NSLayoutConstraint:0x7fe1106386b0 H:[UILabel:0x7fe11062bb80'green'(20)]>",
"<NSLayoutConstraint:0x7fe110638660 UIView:0x7fe11070de50.trailingMargin == UILabel:0x7fe11062bb80'green'.trailing>",
"<NSLayoutConstraint:0x7fe11063fbe0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fe11070de50(375)]>"
I would like to get those constraints to display them in a prettier way (by view key for example). Any idea on how to do it?
Thanks!
My book provides a couple of utility methods (implemented in a category on NSLayoutConstraint) that I like to use to log constraints when I'm debugging or exploring them:
extension NSLayoutConstraint {
class func reportAmbiguity (var v:UIView?) {
if v == nil {
v = UIApplication.sharedApplication().keyWindow
}
for vv in v!.subviews as! [UIView] {
println("\(vv) \(vv.hasAmbiguousLayout())")
if vv.subviews.count > 0 {
self.reportAmbiguity(vv)
}
}
}
class func listConstraints (var v:UIView?) {
if v == nil {
v = UIApplication.sharedApplication().keyWindow
}
for vv in v!.subviews as! [UIView] {
let arr1 = vv.constraintsAffectingLayoutForAxis(.Horizontal)
let arr2 = vv.constraintsAffectingLayoutForAxis(.Vertical)
NSLog("\n\n%#\nH: %#\nV:%#", vv, arr1, arr2);
if vv.subviews.count > 0 {
self.listConstraints(vv)
}
}
}
}
Related
I have a LazyRow. Everything works fine. I just want the items to be randomly rearranged every time this LazyRow is drawn on the screen. Here is my code:
LazyRow(
reverseLayout = true,
contentPadding = PaddingValues(top = twelveDp, end = eightDp)
) {
itemsIndexed(
items = featureUsersLazyPagingItems,
key = { _, featuredPerson ->
featuredPerson.uid
}
) { _, featuredUser ->
featuredUser?.let {
//Daw the age suggested People
DrawSuggestedPerson(featuredUser.toPersonUser(),) {
homeViewModel.deleteFeaturedUserFromLocalDb(featuredUser.uid)
}
}
}
featureUsersLazyPagingItems.apply {
when {
loadState.refresh is LoadState.Loading -> {
item {
ShowLazyColumnIsLoadingProgressBar()
}
}
loadState.append is LoadState.Loading -> {
item {
ShowLazyColumnIsLoadingProgressBar()
}
}
loadState.refresh is LoadState.Error -> {
val e = featureUsersLazyPagingItems.loadState.refresh as LoadState.Error
item {
LazyColumnErrorView(
message = e.error.localizedMessage!!,
onClickRetry = { retry() }
)
}
}
loadState.append is LoadState.Error -> {
val e = featureUsersLazyPagingItems.loadState.append as
LoadState.Error
item {
LazyColumnErrorView(
message = e.error.localizedMessage!!,
onClickRetry = { retry() }
)
}
}
}
}
So the LazyRow displays the same set of 30 or so items but only 3- 4 items are visible on screen, for a bit of variety, I would like the items to be re-arranged so that the user can see different items on the screen. Is there a way to achieve this?
You can shuffle your list inside remember, by doing this you're sure that during one view appearance your order will be the same, and it'll be shuffled on the next view appearance. I'm passing featureUsersLazyPagingItems as a key, so if featureUsersLazyPagingItems changes shuffledItems will be recalculated.
val shuffledItems = remember(featureUsersLazyPagingItems) {
featureUsersLazyPagingItems.shuffled()
}
The only problem of remember is that it'll be reset on screen rotation. Not sure if you need that, and if you wanna save state after rotation, you need to use rememberSaveable. As it can only store simple types, which your class isn't, you can store indices instead, like this:
val shuffledItemIndices = rememberSaveable(featureUsersLazyPagingItems) {
featureUsersLazyPagingItems.indices.shuffled()
}
val shuffledItems = remember(featureUsersLazyPagingItems, shuffledItemIndices) {
featureUsersLazyPagingItems.indices
.map(featureUsersLazyPagingItems::get)
}
I suggest you checkout out documentation for details of how state works in compose.
I try to bind my ImageView using the setImageResource but i got type mismatch error can someone help me please.
override fun onBindViewHolder(holder: RobotViewHolder, position: Int) {
val currentItem = robotList[position]
with(holder.binding) {
robotItemImageView.setImageResource = currentItem.imageResource
categoryNameTextView.text = currentItem.text1
}
You can do it this way,
with(holder.binding) {
robotItemImageView.setImageResource(currentItem.imageResource)
categoryNameTextView.text = currentItem.text1
}
here is my code.
let myDeepLinkAction: UAAction = UAAction(block: {(args:UAActionArguments, handler:UAActionCompletionHandler) -> Void in
handler(UAActionResult.empty())
}, acceptingArguments: {(arguments: UAActionArguments) in
if arguments.situation == UASituation.backgroundPush {
return true
}
return ((arguments.value! as AnyObject).isKind(of: NSString) || (arguments.value! as AnyObject).isKind(of: URL))
})
that type error is coming after swift version conversion 2.2 to 3.0,pls give me solution as possible.
I have found the solution,its simple
let myDeepLinkAction: UAAction = UAAction(block: {(args:UAActionArguments, handler:UAActionCompletionHandler) -> Void in
handler(UAActionResult.empty())
}, acceptingArguments: {(arguments: UAActionArguments) in
if arguments.situation == UASituation.backgroundPush {
return true
}
return (arguments.value! is NSString || arguments.value! is URL)
})
return (arguments.value! is NSString || arguments.value! is URL)
Make use of classForKeyedArchiver property
For eg : If you want to find out if a view controller belongs to a certain class , use the following snippet
if sampleController.isKind( of : listOfFlowersViewController.classForKeyedArchiver()!)
{
//your success code here
}
I was having this issue when I updated to swift 3 in xcode 8
for view in subViews {
if ((view as AnyObject).isKind(of : UIScrollView))
{
scrollView = view as? UIScrollView
}
It was showing error "Cannot call value of non-function type '((AnyClass) -> Bool)!"
Then I added this "classForKeyedArchiver()"
for view in subViews {
if ((view as AnyObject).isKind(of :
UIScrollView().classForKeyedArchiver!))
{
scrollView = view as? UIScrollView
}
Thanks a lot,it worked for me.
We are trying to migrate from UIAutomation to XCUITests.
For the UIAutomation we came up with a handy 'tapOnName' function which just crawled thru a whole sub element tree and tapped on the element with the first match.
function log(msg) {
UIALogger.logDebug(msg);
}
//recursive function crawling thru an elements hierarchy
//and tapping on the first match of accessibilityIdentifier
//or button text
function tapOnNameWithRoot(name,el) {
if (el.name()==name && el.isVisible()) {
log("tap on itt!!!")
el.tap();
return true;
}
if (el.toString()=="[object UIAButton]" && el.label()==name) {
log("tap on Button!!!")
el.tap();
return true;
}
var elements=el.elements();
if (elements===null || elements===undefined) {
log("elements null or undefined for:"+el.toString());
return false;
}
for(var i=0,len=elements.length ;i<len;i++) {
if (tapOnNameWithRoot(name,elements[i])) {
return true;
}
}
return false;
}
var win = UIATarget.localTarget().frontMostApp().mainWindow();
//for ex taps on a button with the text "pushme" in the
//main UIWindow
tapOnNameWithRoot("pushme",win);
No the question : is it possible to implement the same function using XCUIApplication ?
There is shorthand support for this function in XCTest.
For tapping the first match out of any element, you can get all elements and tap the first one:
let app = XCUIApplication()
let element = app.descendentsMatchingType(.Any)["someIdentifier"]
element.tap()
If you know what type of element it is going to be, it's better to filter by that type first:
let app = XCUIApplication()
let element = app.buttons["someIdentifier"]
element.tap()
Are you looking for something like this:
func tapBasedOnAccessibilityIdentifier(elementType elementType: XCUIElementQuery, accessibilityIdentifier: String) {
var isElementExist = false
for element in elementType.allElementsBoundByIndex {
if element.label == accessibilityIdentifier {
element.tap()
isElementExist = true
break
}
}
if !isElementExist {
XCTFail("Failed to find element")
}
}
where you call the method in the test like:
tapBasedOnAccessibilityIdentifier(elementType: app.staticTexts, accessibilityIdentifier: "Accessibility Identifier")
You can tweak it a little so that it cover all the requirements.
I would really appreciate your help with the following. I have been battling this small nuisance for a while now but without luck. I have this bit of code thats basically simulates a AI playing TIC TAC TOE against a player.
let Result = RowCheck(value: 0)
if Result != nil {
println("Computer has two in a row")
var WhereToPlayResult = WhereToPlay(Result.location, pattern: Result.pattern)
if !IsOccupied(WhereToPlayResult) {
SetImageForSpot(WhereToPlayResult, player: 0)
aiDeciding = false
CheckForWin()
return
}
return
}
RowCheck just checks for a pattern to play against.
func RowCheck(#ā€ˇvalue:Int) -> (location:String,pattern:String)? {
var AcceptableFinds = ["011","110","101"]
var FindFuncs = [CheckTop,CheckBottom,CheckLeft,CheckRight,CheckMiddleAcross,CheckMiddleDown,CheckDiagionalRightLeft,CheckDiagionalLeftRight]
for Algorthm in FindFuncs {
var AlgorthmResults = Algorthm(value:value)
if (find(AcceptableFinds,AlgorthmResults.pattern) != nil) {
return AlgorthmResults
}
}
return nil
}
But it gives me an error at:
var WhereToPlayResult = WhereToPlay(Result.location, pattern: Result.pattern)
Because your RowCheck method returns an optional (and might return nil), you need to either unwrap your optional or use a different assignment:
let Result = RowCheck(value: 0)
if Result != nil {
var WhereToPlayResult = WhereToPlay(Result!.location, pattern: Result!.pattern)
// ... ^ ^
}
if let Result = RowCheck(value: 0) {
// ...
}
Side note: only classes should be named starting with a capital letter. To stay within Apple's code style, you should variables and functions as result, rowCheck, etc.