uibutton positioning when resize uifigure

30 ビュー (過去 30 日間)
Justin
Justin 2021 年 8 月 20 日
コメント済み: Justin 2021 年 8 月 23 日
If I create a simple popup window like this:
h = uifigure; h.Position = [0 0 600 400]; centerfig(h)
uiLabel = uilabel(h,'Position',[30 360 540 30],'Text',{'Blah blah ...'});
uiOK = uibutton(h,'Position',[510 30 60 20],'Text','OK');
uiCancel = uibutton(h,'Position',[400 30 90 20],'Text','Cancel');
uit = uitable(h,'Data',rand(10,4),'Position',[30 90 540 250]);
It looks fine. And if I maximise the figure then the table resizes nicely and the label retains its position relative to the figure left. But the OK and Cancel buttons behave differently to each other.
The Cancel button retains its position relative to the figure left, which is what I expected, while the OK button retains its position relative to the figure right leaving a big gap in-between. The OK button behaviour might be desirable (and if they both did this it would save me having to do a resize function!), but I don't understand why the two buttons behave differently. Can someone explain?
  2 件のコメント
Rik
Rik 2021 年 8 月 20 日
You could consider setting the Units property to Normalized. That way it will take up a fraction of your window, regardless of window size.
Justin
Justin 2021 年 8 月 20 日
Thanks. I can do a workaround easily enough though, I was more interested in explanation of why behaviour is different.

サインインしてコメントする。

採用された回答

Himanshu Jain
Himanshu Jain 2021 年 8 月 23 日
As I understand, you have tried to align the 'OK' button with the right side of the UITable, so while resizing, matlab tries to hold that alignment.
When you maximize the figure window, the size of the table adjusts itself which is as expected and as the button is aligned with the right part of the table, the 'OK' button is shifted towards right.
If we talk about the 'Cancel' button, it is not aligned with any other component in the figure so it retains it's Position property even after maximizing the window.
To check the above statement, you can try to change the Position of the 'OK' button so that it does not align with the table. For example, you can execute the below code and try maximizing the window.
h = uifigure; h.Position = [0 0 600 400]; centerfig(h)
uiLabel = uilabel(h,'Position',[30 360 540 30],'Text',{'Blah blah ...'});
uiOK = uibutton(h,'Position',[510 30 50 20],'Text','OK');
uiCancel = uibutton(h,'Position',[400 30 90 20],'Text','Cancel');
uit = uitable(h,'Data',rand(10,4),'Position',[30 90 540 250]);
You will notice that now both the buttons retain their Position property even after maximizing the window.
  1 件のコメント
Justin
Justin 2021 年 8 月 23 日
Thanks for the explanation. It's potentially useful feature that if two components are right aligned, by chance or design, they'll retain that on resize. I couldn't find any mention of this in the documentation for uifigure/uibutton.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by