Get container size for Custom UI Component

조회 수: 10 (최근 30일)
Heine Hørup Pedersen
Heine Hørup Pedersen 2021년 2월 25일
답변: Greg 2021년 4월 15일
I would like to implement costum UI's in the app designer. For simplicity I'm using the IPAddressComponent example.
I'm able to place the UI component in different uifigures and in specified uipanels from command line.
What I can't figure out is how I get the size of the container I'm placing the component inside. Putting a breakpoint in the setup function does not give me any information except the object itself. If I go back in the function call stack I do see a varargin, with the container handles i'm looking for.
% place in uifigure
f1 = uifigure;
f2 = uifigure;
h1 = IPAddressComponent(f1)
% place in uipanel
fig = uifigure;
p = uipanel(fig,'Position',[20 20 300 135]);
h1 = IPAddressComponent(p)
Can any of you tell me how I can get the size/position of the provided container so that I can create my component according to its size?
Regards Heine
  댓글 수: 3
Heine Hørup Pedersen
Heine Hørup Pedersen 2021년 2월 25일
I tried to manpulate with the setup function but any additional arguments are empty...
Maybe it is as you suggest that the funtion is not suposed to see anything passed on to it. Perhaps the way to go is to inherits from the more generic handles class instead? I'll give it a try.
Mario Malic
Mario Malic 2021년 2월 25일
편집: Mario Malic 2021년 2월 25일
I have edited the comment, I am not sure whether it's worthy to chase the implementation from handle classes (i have never worked with it).
Another suggestion would be to add a function in each component to set it to size of parent component so you can later invoke it on all handles that need resizing.

댓글을 달려면 로그인하십시오.

채택된 답변

Heine Hørup Pedersen
Heine Hørup Pedersen 2021년 2월 25일
편집: Heine Hørup Pedersen 2021년 2월 25일
I figured out that I need to use the update method to resize. Which is also clearly stated here:
Thanks Mario
  댓글 수: 2
Mario Malic
Mario Malic 2021년 2월 25일
Can you show a code example as this could be useful for me as well.
Heine Hørup Pedersen
Heine Hørup Pedersen 2021년 2월 25일
I'll post an example when I have figured out exacly how to do it, I just recognized that the parent propertiy was not empty when I reached the update function. So here I should be able to get the container size for the component and size it accoringly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Greg 2021년 4월 15일
I strongly encourage using uigridlayouts. Everywhere. Every componentcontainer I build starts with a grid, and every app has a grid at the base of the figure. Using grids makes nearly all conversations about size and position irrelevant.
To your specific question though, the setup method executes before any input arguments are processed. To obtain the parent of the componentcontainer, you need to do it in the constructor, or the update method. In either of those two methods, a simple obj.Parent.Position will give you the information you want.


Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by