Why do I need to add 44 to UITextView frame height? - height

I'm trying to change the height of a UITextView dynamically:
... UITextView* textView; ...
CGRect textViewNewFrame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, 130);
textView.frame = textViewNewFrame;
In IB, I have a UIButton top-aligned with my UITextView. The UIButton is 130px high; the UITextView is, say, 150px high.
If I run the code above, the UITextView ends up 44px too short. I have to change my CGRectMake line to end,
..., 130 + 44);
However, if in IB I 'hard-code' the height of the UITextView to 130px, the UIButton and UITextView are rendered both top- and bottom-aligned (they are both the same height).
So when changing the UITextView's frame's height dynamically I have to add 44, but not when 'statically' setting its height via IB.
Oddly, UILabel doesn't misbehave like this (I don't have to add 44px when dynamically resizing).
I know that 44 is a magic number in IB (the height of the nav bar etc) but why do I need to add it? Am I missing something/doing something wrong, or is this just an anomaly? I'm concerned that this must be such a common operation and yet I can't find anyone else asking the same question and coming to the same conclusion.

Related

Why does decreasing y position by 1 point causes the Text view to jump by 47 points when having the IgnoringSafeArea enabled in SwiftUI?

I am trying to put a message on the blue line (the safe area box shown in the image). I need to have the .edgesIgnoringSafeArea(.all). The problem is that as soon as I change the Y position to anything under 11, the text jumps 47 points up to the position shown in the image. Does anybody know what is causing this jump?
I am putting both of the cases here with the resulting screenshot. I would appreciate it if anyone could help me understand why that 1-point difference in the Y position causes the "First" message to print 47 points higher than the "Second" message.
struct TestView: View {
var body: some View {
GeometryReader{ geo in
Text("First")
.edgesIgnoringSafeArea(.all)
.position(x: 50, y: 10)
Text("Second")
.edgesIgnoringSafeArea(.all)
.position(x: 50, y: 11)
}
}}
If I’m not mistaken it’s the order of the modifiers. Everything after edgesIgnoringSafeArea still ignores Safe area.
That modifier wrapped everything that came before it.

SwiftUI - Horizontal ScrollView becomes Vertically scrollable

I have a problem with SwiftUI's ScrollView. If you run the app on a smaller screen (iPhone SE 1st gen for example), the ScrollView (horizontal) goes out of the screen because it doesn't fit its height on it. Therefore it becomes scrollable vertically also. I have an outer scroll view (vertical) therefore I don't want the inner ScrollView to be able to scroll vertically. Any ideas on how can I fix this?
ScrollView(.vertical) { // <------ Outher one
Text("Welcome")
ScrollView(.horizontal) { // <------ Inner one (if it doesnt fit on screen it becomes vertically scrollable
HStack {
Content...
}
}
}
Thanks!

QML Tableview horizontal scrollbar

I am using TableView QML component, where each column width is set to the table total width divided by number of columns. This introduces some fraction parts and in result, a horizontal scrollbar is almost always visible in the bottom.
But I don't want to disable scrollbar as I have ability of changing column width and because of that scrollbar may be needed. So my problem is about initial showing of this TableView component. I want to get rid of that scrollbar when component is shown first time, but don't disable it for future usage.
I am new to QML and don't know all aspects of it, but solution that came to me is to set width of each column as I do, then for the last one get sum of all previous columns and set it's width to parent.width - that sum. Can you please help me to understand how can I implement this using QML?
columnWidthProvider is calculated once at adding column. I cant reproduce your bug with this. contentWidth become same as width
TableView {
id: table
anchors.fill: parent
columnWidthProvider: function (column) {
console.log(parent.width , columns, parent.width / columns)
// wdth is 0 here
return parent.width / columns
}
ScrollBar.horizontal: ScrollBar {
policy: ScrollBar.AsNeeded
}
//....
}
But i had same issue in another view. Solution was:
Flickable {
id:view
ScrollBar.horizontal: ScrollBar {
policy: (view.width - view.contentWidth < -3) ?
ScrollBar.AlwaysOff : ScrollBar.AsNeeded}
}
}

Changing canvas scroll region on Python turtle canvas

Can I change the scrollregion on a Python turtle canvas? I want the drawing to move with it, not just the coordinates to shift. The appearance I'm going for is side-scroller like, where the screen's display region moves to center the turtle onscreen.
I've tried using turtle.setworldcoordinates(llx, lly, urx, ury), but, from the documentation, "This performs a screen.reset()". I've also looked at this SO question , but this involves scroll bars, will not center the turtle easily, and has a limited canvas space. What I'm looking for is something that:
Moves the display region to center the turtle
Also moves the drawing
Has an infinite scroll region
does not display scroll bars
can be called quickly with a function
My best guess would be to be able to have an infinite scrolled canvas somehow, then hide the scroll bars and set them according to turtle position.
Is this possible in Python 2.7? I don't mind if it uses tkinter as well.
EDIT: 6-3-15
I found the canvas.xview and canvas.yview functions, but they don't seem to work once I define screen = turtle.TurtleScreen(canvas), and TurtleScreen has no xview or yview functions. I can't seem to make this work.
Then I found turtle.ScrolledCanvas(). This seems ideal except it has no methods for setting scroll manually from the program. Can I set the scroll manually on a turtle.ScrolledCanvas()???
The position of a canvas can be changed without a reset using canvas.place() method. It will move the turtle and the drawings too, so the turtle needs to be relocated after every move.
The next code moves the canvas with Left and Right arrows and draws a circle with Space, while keeping the turtle in the center. No ScrolledCanvas needed, just a very large standard canvas:
import turtle
import Tkinter as tk
def keypress(event):
global xx, canvas, t, speed
ev = event.keysym
if ev == 'Left':
xx += speed
else:
xx -= speed
canvas.place(x=xx)
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
return None
def drawCircle(_):
global t
t.pendown()
t.fillcolor(0, 0, 1.0)
t.fill(True)
t.circle(100)
t.fill(False)
t.fillcolor(0, 1, 0)
t.penup()
# Set the main window
window = tk.Tk()
window.geometry('500x500')
window.resizable(False, False)
# Create the canvas. Width is larger than window
canvas = turtle.Canvas(window, width=2000, height=500)
xx = -500
canvas.place(x=xx, y=0)
# Bring the turtle
t = turtle.RawTurtle(canvas)
t.shape('turtle') # nicer look
t.speed(0)
t.penup()
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
# key binding
window.bind('<KeyPress-Left>', keypress)
window.bind('<KeyPress-Right>', keypress)
window.bind('<KeyPress-space>', drawCircle)
drawCircle(None)
speed = 3 # scrolling speed
window.mainloop()
Having a real infinite scrolling would require to redraw every item in the canvas every time with the required offset, instead of actually moving or scrolling the canvas. Functions like create_image() can give the illusion of movement with static backgrounds, but it resets the drawings.

Overlapping MenuItems in Cocos2d CCMenu

I am trying to make a Menu that contains 13 MenuItemImages in two columns (the last one is in the middle).
The frame width/ design resolution width is 480 pixels. MenuItemImage width is 180 pixels.
here is my code :
CCMenu* testMenu = CCMenu::createWithArray(testMenuItems);
testMenu->alignItemsInColumns(2,2,2,2,2,2,1);
CCSize size1 = CCDirector::sharedDirector()->getWinSize();
testMenu->setPosition(ccp(size1.width / 2, size1.height/2));
but the two columns are slightly overlapping. (the right one is above the left one)
here is the result of my code:
I would like it to be properly spaced with some padding between the two columns.
Please help me out I am new to Cocos2d-x.
the alignItemsInColumns will align menu itens based on itens center, against menu width.
In your case, you have 2 options:
1) Increase your menu width (by default, their size will be based on screen size. Change the menu.contentSize.width)
2) Change the anchor point of left itens to ccp(.7,.5) and right itens to ccp(.3,.5) for example